本章主要介绍了树另外两个特性,分别是重心和最近公共祖先。1、求解重心 2、求解树中节点到其他所有节点的和的最小值 3、LCA问题
树的重心1 (1 <= n <= 1000)
//树的重心I DFS
#include<bits/stdc++.h>
using namespace std;
int n, cnt;
vector<int> edges[1001];
int pre[1001], f[1001], dis[1001];
queue<int> q;
inline void solve(int x){
cnt++;
for (auto y : edges[x]){
if (pre[x] != y){
pre[y] = x;
solve(y);
}
}
}
inline void bfs(){
while (!q.empty()){
int x = q.front();
q.pop();
for (auto y : edges[x]){
if (pre[x] != y){
pre[y] = x;
dis[y] = dis[x] + 1;
q.push(y);
}
}
}
}
int main(){
scanf("%d", &n);
for (int i = 1; i < n; i++){
int x, y;
scanf("%d%d", &x, &y);
edges[x].push_back(y);
edges[y].push_back(x);
}
for (int i = 1; i <= n; i++){
f[i] = 0;
memset(pre, 0, sizeof(pre));
for (auto y : edges[i]){
cnt = 0;
pre[y] = i;
solve(y);
f[i] = max(f[i], cnt);
}
}
int idex = 0, v = 1 << 30;
for (int i = 1; i <= n; i++){
if (f[i] <= v){
v = f[i], idex = i;
}
}
memset(pre, 0, sizeof(pre));
memset(dis, 0, sizeof(dis));
pre[idex] = -1;
q.push(idex);
bfs();
int ans = 0;
for (int i = 1; i <= n; i++){
ans += dis[i];
}
printf("%d\n", ans);
}
//树的重心I BFS
#include<bits/stdc++.h>
using namespace std;
int n, cnt;
vector<int> edges[1001];
int pre[1001], f[1001], dis[1001];
queue<int> q;
inline void solve(){
while (!q.empty()){
int x = q.front();
q.pop();
for (auto y : edges[x]){
if (pre[x] != y){
cnt++;
pre[y] = x;
q.push(y);
}
}
}
}
inline void bfs(){
while (!q.empty()){
int x = q.front();
q.pop();
for (auto y : edges[x]){
if (pre[x] != y){
pre[y] = x;
dis[y] = dis[x] + 1;
q.push(y);
}
}
}
}
int main(){
scanf("%d", &n);
for (int i = 1; i < n; i++){
int x, y;
scanf("%d%d", &x, &y);
edges[x].push_back(y);
edges[y].push_back(x);
}
for (int i = 1; i <= n; i++){
memset(pre, 0, sizeof(pre));
for (auto y : edges[i]){
cnt = 0;
pre[y] = i;
q.push(y);
solve();
f[i] = max(f[i], cnt);
}
}
int idex = 0, v = 1 << 30;
for (int i = 1; i <= n; i++){
if (f[i] <= v){
v = f[i], idex = i;
}
}
memset(pre, 0, sizeof(pre));
memset(dis, 0, sizeof(dis));
pre[idex] = -1;
q.push(idex);
bfs();
int ans = 0;
for (int i = 1; i <= n; i++){
ans += dis[i];
}
printf("%d\n", ans);
}
树的重心2 (1 <= n <= 100000)
//树的重心II
#include<bits/stdc++.h>
using namespace std;
int n, cnt;
vector<int> edges[100001];
int pre[100001], f[100001], dis[100001], s[100001];
queue<int> q;
inline void solve(int x){
s[x] = 1;
for (auto y : edges[x]){
if (pre[x] != y){
pre[y] = x;
solve(y);
s[x] += s[y];
}
}
}
inline void bfs(){
while (!q.empty()){
int x = q.front();
q.pop();
for (auto y : edges[x]){
if (pre[x] != y){
pre[y] = x;
dis[y] = dis[x] + 1;
q.push(y);
}
}
}
}
int main(){
scanf("%d", &n);
for (int i = 1; i < n; i++){
int x, y;
scanf("%d%d", &x, &y);
edges[x].push_back(y);
edges[y].push_back(x);
}
pre[1] = -1;
solve(1);
int idex = 0, v = 1 << 30, f;
for (int i = 1; i <= n; i++){
f = 0;
for (auto y : edges[i]){
if (pre[i] != y){
f = max(f, s[y]);
}
else {
f = max(f, n - s[i]);
}
}
if (f < v){
v = f, idex = i;
}
}
memset(pre, 0, sizeof(pre));
q.push(idex);
pre[idex] = -1;
bfs();
long long ans = 0;
for (int i = 1; i <= n; i++){
ans += dis[i];
}
printf("%lld\n", ans);
}
树上LCA1 (1 <= n, m <= 1000)
//树上LCA1 (1 <= n,m <= 1000)
#include<bits/stdc++.h>
using namespace std;
int n, m, cnt;
vector<int> edges[1001];
int pre[1001], f[1001], dis[1001], s[1001], fa[1001];
queue<int> q;
inline void solve(int x){
s[x] = 1;
for (auto y : edges[x]){
if (pre[x] != y){
pre[y] = x;
solve(y);
s[x] += s[y];
}
}
}
inline void bfs(){
while (!q.empty()){
int x = q.front();
q.pop();
for (auto y : edges[x]){
if (pre[x] != y){
pre[y] = x;
dis[y] = dis[x] + 1;
q.push(y);
}
}
}
}
int main(){
scanf("%d", &n);
for (int i = 1; i < n; i++){
int x, y;
scanf("%d%d", &x, &y);
edges[x].push_back(y);
fa[y] = x;
}
q.push(1);
bfs();
scanf("%d", &m);
for (int i = 1; i <= m; i++){
int x, y;
scanf("%d%d", &x, &y);
if (dis[x] < dis[y]){
swap(x, y);
}
int z = dis[x] - dis[y];
for (int j = 1; j <= z; j++){
x = fa[x];
}
while (x != y){
x = fa[x], y = fa[y];
}
printf("%d\n", x);
}
}
树上LCA2 (1 <= n,m <= 100000)
//树上LCA2 (1 <= n,m <= 100000)
#include<bits/stdc++.h>
using namespace std;
int n, m, cnt;
vector<int> edges[100001];
int pre[100001], f[100001], dis[100001], s[100001], fa[100001][21];
queue<int> q;
inline void solve(int x){
s[x] = 1;
for (auto y : edges[x]){
if (pre[x] != y){
pre[y] = x;
solve(y);
s[x] += s[y];
}
}
}
inline void bfs(){
while (!q.empty()){
int x = q.front();
q.pop();
for (auto y : edges[x]){
if (pre[x] != y){
pre[y] = x;
dis[y] = dis[x] + 1;
q.push(y);
}
}
}
}
int main(){
scanf("%d", &n);
for (int i = 1; i < n; i++){
int x, y;
scanf("%d%d", &x, &y);
edges[x].push_back(y);
fa[y][0] = x;
}
for (int i = 1; i <= 20; i++){
for (int j = 0; j <= n; j++){
fa[j][i] = fa[fa[j][i - 1]][i - 1];
}
}
q.push(1);
bfs();
scanf("%d", &m);
for (int i = 1; i <= m; i++){
int x, y;
scanf("%d%d", &x, &y);
if (dis[x] < dis[y]){
swap(x, y);
}
int z = dis[x] - dis[y];
for (int j = 0; j <= 20 && z; j++, z /= 2){
if (z & 1){
x = fa[x][j];
}
}
if (x == y){
printf("%d\n", x);
continue;
}
for (int i = 20; i >= 0; i--){
if (fa[x][i] != fa[y][i]){
x = fa[x][i], y = fa[y][i];
}
}
printf("%d\n", fa[x][0]);
}
}