1. 最小距离和(树的重心)
题目: http://oj.daimayuan.top/course/7/problem/529
#include<bits/stdc++.h>
using namespace std;typedef long long ll;const int N = 2e5+5;
const int inf = 0x3f3f3f3f;int n;
int pre[N];
int dist[N];
vector<int> e[N];
int f[N];
int cnt;//求出每个节点到根节点的距离
void dfs(int x){ for(auto y:e[x]){if(y!=pre[x]){pre[y]=x;dist[y]=dist[x]+1;dfs(y);}}
}//计算以当前节点为根节点的子树中的最大深度
void solve(int x){cnt++;for(auto y:e[x]){if(y!=pre[x]){pre[y]=x;solve(y);}}
}int main(){ios::sync_with_stdio(false);cin.tie(nullptr);cin>>n;for(int i=1;i<n;i++){int x,y;cin>>x>>y;e[x].push_back(y);e[y].push_back(x);}//对每个节点,计算其子树中的最大深度for(int i=1;i<=n;i++){f[i]=0;memset(pre,0,sizeof(pre));for(auto y:e[i]){cnt=0;pre[y]=i;solve(y);f[i]=max(f[i],cnt);}}//找到直径的根节点,即子树中最大深度的最小的节点int idx=0,v=1<<30;for(int i=1;i<=n;i++){if(f[i]<v){v=f[i];idx=i;}}memset(dist,0,sizeof(dist));memset(pre,0,sizeof(pre));pre[idx]=-1;dfs(idx);v=0;//计算以idx为根节点的最小距离和int ans=0;for(int i=1;i<=n;i++){ans+=dist[i];}cout<<ans<<"\n";return 0; }
2. 树的最近公共祖先 (LCA)
题目: http://oj.daimayuan.top/course/7/problem/531
#include<bits/stdc++.h>
using namespace std;typedef long long ll;const int N = 1001;
const int inf = 0x3f3f3f3f;int n,m;
int dist[N];
vector<int> e[N];
int fa[N];//求出每个点与根节点的距离
void dfs(int x){for(auto y:e[x]){dist[y]=dist[x]+1;dfs(y);}
}int main(){ios::sync_with_stdio(false);cin.tie(nullptr);cin>>n;for(int i=1;i<n;i++){int x,y;cin>>x>>y;e[x].push_back(y);fa[y]=x;}memset(dist,0,sizeof(dist));dfs(1);cin>>m;for(int i=1;i<=m;i++){int x,y;cin>>x>>y;if(dist[x]<dist[y]){swap(x,y);}int z=dist[x]-dist[y];for(int j=1;j<=z;j++){x=fa[x];}while(x!=y){x=fa[x];y=fa[y];}cout<<x<<"\n";}return 0; }