A
模板
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
//#define int long long
int ma,id;
int n;
vector<int> op[N];
void dfs(int u,int fa,int o){
if(o>ma){
ma=o;
id=u;
}
for(int i=0;i<op[u].size();i++){
int v=op[u][i];
if(v!=fa){
dfs(v,u,o+1);
}
}
}
//unsigned
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n-1;i++){
int x,y;
cin>>x>>y;
op[x].push_back(y);
op[y].push_back(x);
}
dfs(1,0,0);
ma=0;
dfs(id,0,0);
cout<<ma;
return 0;
}B
用单调倍增处理跳转
优先跳转至两项深度一支,然后同时跳转
如果跳转后相同,不能跳转,因为不知道是不是最近的;
如果跳转后不同,可以跳转,最后向上爬一层就行
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
//#define int long long
int n,T,s;
int st[N][25];
int deg[N];
vector<int> op[N];
void chu(int u,int fa){
st[u][0]=fa;
for(int i=1;i<=20;i++){
st[u][i]=st[st[u][i-1]][i-1];
}
deg[u]=deg[fa]+1;
for(int i=0;i<op[u].size();i++){
int v=op[u][i];
if(v!=fa){
chu(v,u);
}
}
}
int LCA(int x,int y){
if(deg[x]<deg[y]) swap(x,y);
for(int i=20;i>=0;i--){
if(deg[st[x][i]]>=deg[y]){
x=st[x][i];
}
}
if(x==y) return x;
for(int i=20;i>=0;i--){
if(st[x][i]!=st[y][i]){
x=st[x][i];y=st[y][i];
}
}
return st[x][0];
}
//unsigned
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>T>>s;
for(int i=1;i<=n-1;i++){
int x,y;
cin>>x>>y;
op[x].push_back(y);
op[y].push_back(x);
}
chu(s,0);
while(T--){
int x,y;
cin>>x>>y;
cout<<LCA(x,y)<<'\n';
}
return 0;
}C
公式:sum=deg[x]+deg[y]-2*deg[LCA(x,y)]
原理:x的深度+y的深度=LCA的深度*2+最短距离
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10;
//#define int long long
int ma;
int n,T,s=1;
int st[N][22];
int deg[N];
vector<int> op[N];
void chu(int u,int fa){
st[u][0]=fa;
for(int i=1;i<=20;i++){
st[u][i]=st[st[u][i-1]][i-1];
}
deg[u]=deg[fa]+1;
for(int i=0;i<op[u].size();i++){
int v=op[u][i];
if(v!=fa){
chu(v,u);
}
}
}
int LCA(int x,int y){
if(deg[x]<deg[y]) swap(x,y);
for(int i=20;i>=0;i--){
if(deg[st[x][i]]>=deg[y]){
x=st[x][i];
}
}
if(x==y) return x;
for(int i=20;i>=0;i--){
if(st[x][i]!=st[y][i]){
x=st[x][i];y=st[y][i];
}
}
return st[x][0];
}
//unsigned
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n-1;i++){
int x,y;
cin>>x>>y;
op[x].push_back(y);
op[y].push_back(x);
}
chu(s,0);
cin>>T;
while(T--){
int x,y;
cin>>x>>y;
int sum=0;
sum=deg[x]+deg[y]-2*deg[LCA(x,y)];
cout<<sum<<"\n";
}
return 0;
}D
公式:以这个点为根的子树的总点数*2-1+枝的排列组合出来的总点数
#include<bits/stdc++.h>
using namespace std;
const int N=5e4+10;
//#define int long long
int n,s,m;
vector<int> op[N];
int a[N];
int ans[N];
int chu(int u,int fa){
a[u]=1;
for(int i=0;i<op[u].size();i++){
int v=op[u][i];
if(v!=fa){
a[v]=chu(v,u);
a[u]+=a[v];
}
}
for(int i=0;i<op[u].size();i++){
int v=op[u][i];
for(int j=i+1;j<op[u].size();j++){
int w=op[u][j];
if(v!=fa&&w!=fa) ans[u]+=(a[v]*a[w]);
}
}
ans[u]*=2;
//cout<<u<<' '<<ans[u]<<'\n';
ans[u]+=(2*a[u]-1);
//cout<<u<<' '<<a[u]<<'\n';
return a[u];
}
//unsigned
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>s>>m;
for(int i=1;i<=n-1;i++){
int x,y;
cin>>x>>y;
op[x].push_back(y);
op[y].push_back(x);
}
chu(s,0);
while(m--){
int x;
cin>>x;
cout<<ans[x]<<"\n";
}
return 0;
}E
原理:树上两条路径如果交叉,LCA一定是其中一个交点
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10;
//#define int long long
int ma;
int n,T,s=1;
int st[N][22];
int deg[N];
vector<int> op[N];
void chu(int u,int fa){
st[u][0]=fa;
for(int i=1;i<=20;i++){
st[u][i]=st[st[u][i-1]][i-1];
}
deg[u]=deg[fa]+1;
for(int i=0;i<op[u].size();i++){
int v=op[u][i];
if(v!=fa){
chu(v,u);
}
}
}
int LCA(int x,int y){
if(deg[x]<deg[y]) swap(x,y);
for(int i=20;i>=0;i--){
if(deg[st[x][i]]>=deg[y]){
x=st[x][i];
}
}
if(x==y) return x;
for(int i=20;i>=0;i--){
if(st[x][i]!=st[y][i]){
x=st[x][i];y=st[y][i];
}
}
return st[x][0];
}
int oiiai(int x,int y){
int lca=LCA(x,y);
return deg[lca]-deg[x]+(deg[lca]-deg[y]);
}
//unsigned
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>T;
for(int i=1;i<=n-1;i++){
int x,y;
cin>>x>>y;
op[x].push_back(y);
op[y].push_back(x);
}
chu(s,0);
while(T--){
int a,b,c,d;
cin>>a>>b>>c>>d;
int p1=LCA(a,b),p2=LCA(c,d);
bool f1=oiiai(a,p2)+oiiai(b,p2)==oiiai(a,b);
bool f2=oiiai(c,p1)+oiiai(d,p1)==oiiai(c,d);
if(f1||f2) cout<<"Y\n";
else cout<<"N\n";
}
return 0;
}