树
树的重心
定义:对于一棵无根树而言,一个节点被选为根节点后,其子树包含节点最大值最小得点为树的重心。一棵树有1-2两个重心。
性质:
当重心为根节点时,他底下每个子树的大小不大于整颗树大小的一半。
重心到其他所有节点的距离和最小。
最近公共祖先
一般求LCA的方法:
先计算出u,v的深度,将u,v调整到同一个深度(取深度小的),两个节点一起逐级往上跳,直到两节点相等。
给你一棵树,树以下列方式给出:
树以下列方式给出:
- 编号为 11 的节点为根;
- 输入第一行给出一个数 nn,表示一共有 nn 个节点;
- 接下来 n−1n−1 行,每行给出两个数 x,y(x≠y)x,y(x≠y),表示 x,yx,y 之间有一条边。
接下来有 mm 组询问,每组询问有两个数 x,y(x≠y)x,y(x≠y), 你需要求出 x,yx,y 的最近公共祖先。
例题
输入格式
见题面。
输出格式
输出共 mm 行,每行 11 个数表示每次询问答案。
样例输入
4
1 2
1 3
3 4
2
1 2
2 4
样例输出
1
1
数据规模
对于所有数据,保证1≤n,m≤1000,1≤x,y≤n,x≠y1≤n,m≤1000,1≤x,y≤n,x≠y。
#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define pob pop_back
#define mem(a,b) memset(a,b,sizeof(a))
#define all(a) (a).begin(),(a).end()
#define debug(a) cout<<#a<<"="<<a<<endl;
inline ll rr(){ll f=1,x=0;char ch;do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');return f*x;}
using namespace std;
const ll INF=0x3f3f3f3f,inf=0x3f3f3f3f3f3f3f;
const int maxn=1e5+6;
int n;
int deep[maxn],father[maxn];
std::vector<int> edge[maxn];
void init() {
n=rr();
for(int i=1;i<n;i++) {
int u=rr(),v=rr();
edge[u].pb(v);
father[v]=u;
}
}
void dfs(int u) { // 求出深度
for(auto son:edge[u]) {
deep[son]=deep[u]+1;
dfs(son);
}
}
int main(){
init();
dfs(1);
int q=rr();
while (q--) {
int u=rr(),v=rr();
if(deep[u]<deep[v]) {
swap(u,v);
} // u的深度一定>=v
int z=deep[u]-deep[v];
while (z--) u=father[u]; // 使两者深度一致
while (v!=u) v=father[v],u=father[u];
std::cout << u << '\n';
}
}
此题只是用于理解,效率较低。在保持深度一致的操作上效率低,最坏可以到On复杂度;两个节点都在向上跳的过程中,效率也低,最坏可以达到On。所以要提高效率就要从这两方面入手。
用f[x][y]表示x节点向上跳2y步后的节点。
有递推公式:f[x][y]=f[f[x][y-1]][y-1];
例题
给你一棵树,树以下列方式给出:
树以下列方式给出:
- 编号为 11 的节点为根;
- 输入第一行给出一个数 nn,表示一共有 nn 个节点;
- 接下来 n−1n−1 行,每行给出两个数 x,y(x≠y)x,y(x≠y),表示 x,yx,y 之间有一条边。
接下来有 mm 组询问,每组询问有两个数 x,y(x≠y)x,y(x≠y), 你需要求出 x,yx,y 的最近公共祖先。
输入格式
见题面。
输出格式
输出共 mm 行,每行 11 个数表示每次询问答案。
样例输入
4
1 2
1 3
3 4
2
1 2
2 4
样例输出
1
1
数据规模
对于所有数据,保证1≤n,m≤100000,1≤x,y≤n,x≠y1≤n,m≤100000,1≤x,y≤n,x≠y。
代码
#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define pob pop_back
#define mem(a,b) memset(a,b,sizeof(a))
#define all(a) (a).begin(),(a).end()
#define debug(a) cout<<#a<<"="<<a<<endl;
inline ll rr(){ll f=1,x=0;char ch;do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');return f*x;}
using namespace std;
const ll INF=0x3f3f3f3f,inf=0x3f3f3f3f3f3f3f;
const int maxn=1e5+6;
int n;
int deep[maxn],father[maxn][22];
std::vector<int> edge[maxn];
void init() {
n=rr();
for(int i=1;i<n;i++) {
int u=rr(),v=rr();
edge[u].pb(v);
father[v][0]=u;
}
}
void dfs(int u) {
for(auto son:edge[u]) {
deep[son]=deep[u]+1;
dfs(son);
}
}
int main(){
init();
dfs(1);
for(int i=1;i<=20;i++) {
for(int j=1;j<=n;j++)
// 这个点必须先存在
if(father[j][i-1]) father[j][i]=father[father[j][i-1]][i-1];
}
int q=rr();
while (q--) {
int u=rr(),v=rr();
if(deep[u]<deep[v]) {
swap(u,v);
} // u的深度一定>=v
int z=deep[u]-deep[v];
for(int j=0; j<=20 && z; j++,z>>=1) {
if(z&1) u=father[u][j];
} // 一个是多个2的n次方的数的和,跳的代价变为logn
if(u == v) {
std::cout << u << '\n';
continue;
}
for(int j=20;j>=0;j--) {
if(father[u][j]!=father[v][j])
u=father[u][j],v=father[v][j];
} // 最后停止的地方是LCA的下面
std::cout << father[u][0] << '\n';
}
}