树的直径【树的重心 / 树的质心】(填坑中)
定义:
对于一棵树n个节点的无根树,找到一个点,将树变成以该点为根的有根树,而重心则是删除某节点 Node 之后能使得最大子树的结点数最小的节点。
性质:
1.删除重心后所得的所有子树,节点数不超过原树的1/2,一棵树最多有两个相邻的重心;
2.树中所有节点到重心的距离之和最小,如果有两个重心,那么他们距离之和相等;
3.两个树通过一条边合并,新的重心在原树两个重心的路径上;
4.树删除或添加一个叶子节点,重心最多只移动一条边;
1)Balancing Act
题意:
对于N个节点N-1条边相连接的树,问这棵树的重心和对应的最大子树的节点数为多少
题解:
#include <iostream> #include <map> #include <vector> #include <queue> #include <string> #include <set> #include <algorithm> #include <cstring> #include <string> #include <vector> #include <map> #include <set> #include <list> #include <deque> #include <queue> #include <stack> #include <cstdlib> #include <cstdio> #include <cmath> #include <iomanip> #pragma GCC optimize(2) using namespace std; const int inf=0x3f3f3f3f; #define ll long long #define ull unsigned long long const int mod = 1e9+7;/// 998244353; const int mxn = 2e4 +7; const int N = 8e4 + 7 ; int _ , n , m , t , k , ans , cnt , si ; template <class T> void rd(T &x){ T flag = 1 ; x = 0 ; char ch = getchar() ; while(!isdigit(ch)) { if(ch=='-') flag = -1; ch = getchar(); } while(isdigit(ch)) { x = (x<<1) + (x<<3) + (ch^48); ch = getchar(); } x*=flag; } vector<int> G[mxn] ; int deg[mxn] , mn_son = 0 , mn_node = 0 , head[mxn<<1] , to[mxn<<1] , nx[mxn<<1] ; void add(int u,int v) { to[cnt] = v , nx[cnt] = head[u] , head[u] = cnt++; } void DFS(int x,int par) { deg[x]++ ; int mx_son = 0 ; for(int i=head[x];~i;i=nx[i]){ int v = to[i] ; if(v==par) continue ; DFS(v,x); deg[x] += deg[v] ; mx_son = max(mx_son , deg[v]); } mx_son = max(mx_son , n-deg[x]) ; if(mn_son > mx_son) mn_node = x , mn_son = mx_son ; } void solve() { for(rd(t);t;t--){ rd(n) ; mn_node = 0 , mn_son = mod ; cnt = 0 ; memset(head,-1,sizeof(head)); memset(deg,0,sizeof(deg)); for(int i=1,u,v;i<n;i++){ rd(u) , rd(v) ; add(u,v) , add(v,u) ; } DFS(1,0); printf("%d %d\n",mn_node,mn_son ); } } int main() { ios::sync_with_stdio(false); cin.tie(0) ; cout.tie(0); solve(); }
2)Cotree
题意:
由N个点构成两棵树,问在两颗树之间连接一条边之后,各点之间距离和的最小值为多少
题解:
进行两次DFS找到两颗树的重心,将两个重心连接起来,再进行一次DFS求出距离和即可
#include <iostream> #include <map> #include <vector> #include <queue> #include <string> #include <set> #include <algorithm> #include <cstring> #include <string> #include <vector> #include <map> #include <set> #include <list> #include <deque> #include <queue> #include <stack> #include <cstdlib> #include <cstdio> #include <cmath> #include <iomanip> #pragma GCC optimize(2) #define ll long long #define ull unsigned long long using namespace std; template <class T> void rd(T &x){ x = 0 ;T flag = 1 ;char ch = getchar(); while(!isdigit(ch)) { if(ch=='-') flag = -1 ; ch = getchar() ; } while(isdigit(ch)) { x = (x<<1) + (x<<3) + (ch^48) ; ch = getchar(); } x*= flag ; } const int INF=0x3f3f3f3f; const int mxn = 1e5+7; ll t,n,m,k,cnt,node_1,node_2,ans_1,ans_2,mn_son = 0 , mn_node = 0 , res , ans ; int head[mxn<<2] , to[mxn<<2] , nx[mxn<<2] ,deg[mxn<<2] ; int vis[mxn<<2] ; void add(int u,int v){ to[cnt] = v , nx[cnt] = head[u] , head[u] = cnt++; } void DFS_INIT(int x) /// 计算两颗子树的节点数量 { if(vis[x]) return ; res++; vis[x] = 1 ; for(int i=head[x];~i;i=nx[i]) if(!vis[to[i]]) DFS_INIT(to[i]); /// } void DFS(int x,int par,int n) { deg[x]=1; int mx_son = 0 ; for(int i=head[x];~i;i=nx[i]){ int v = to[i] ; if(v==par) continue; DFS(v,x,n); deg[x] += deg[v] ; mx_son = max(mx_son , deg[v]);/// 子树的最大节点个数 } mx_son = max(mx_son,n-deg[x]); /// 子树的最大节点个数 if( mx_son < mn_son ){ mn_son = mx_son , mn_node = x ; } } void DFS_END(int x,int par) { deg[x] = 1 ; for(int i=head[x];~i;i=nx[i]){ int v = to[i] ; if(v==par) continue; DFS_END(v,x) ; deg[x] += deg[v]; ans+=(ll)(deg[v])*(ll)(n-deg[v]); } } int main() { while(~scanf("%lld",&n)){ cnt = 0 ; memset(deg,0,sizeof(deg)); memset(head,-1,sizeof(head)); for(int i=1,u,v;i<=n-2;i++){ rd(u) , rd(v) ; add(u,v) , add(v,u) ; } memset(vis,0,sizeof(vis)); int n1 = 0 , n2 = 0 ; res = 0 ; DFS_INIT(1) , node_1 = 1 , n1 = res , res = 0 ; for(int i=1;i<=n;i++){ if(!vis[i]) { DFS_INIT(i) , n2 = res , node_2 = i , res = 0 ; break; } } mn_son = INF ; int e1 , e2 ; DFS(node_1,0,n1) , e1 = mn_node ,mn_son = INF ; DFS(node_2,0,n2) , e2 = mn_node ,mn_son = INF ; ///cout<<e1<<" -- "<<e2<<endl; add(e1,e2) , add(e2,e1); ans = 0 ; DFS_END(1,0); printf("%lld\n",ans); } return 0; }
3)C. Link Cut Centroids
题意:
在N个点的树中,通过删除一条边,连接一条边使得新树只有一个重心,并输出删除和连接的点
题解:
通过DFS判断存在几个重心,然后找到一个重心的子树的叶子节点,如果是一个重心,那么输出两遍叶子节点和连接的点;如果是两个重心,则将找到的叶子节点连接到另一个重心上
#include <iostream> #include <map> #include <vector> #include <queue> #include <string> #include <set> #include <algorithm> #include <cstring> #include <string> #include <vector> #include <map> #include <set> #include <list> #include <deque> #include <queue> #include <stack> #include <cstdlib> #include <cstdio> #include <cmath> #include <iomanip> #pragma GCC optimize(2) using namespace std; const int inf=0x3f3f3f3f; #define ll long long #define ull unsigned long long const int mod = 1e9+7;/// 998244353; const int mxn = 1e5 +7; int _ , n , m , t , k , ans , cnt , si , res ; template <class T> void rd(T &x){ T flag = 1 ; x = 0 ; char ch = getchar() ; while(!isdigit(ch)) { if(ch=='-') flag = -1; ch = getchar(); } while(isdigit(ch)) { x = (x<<1) + (x<<3) + (ch^48); ch = getchar(); } x*=flag; } vector<int> G[mxn] ; int deg[mxn] , _son = 0 , _node = 0 , head[mxn<<1] , to[mxn<<1] , nx[mxn<<1] , id[5] , node ; void add(int u,int v){ to[cnt] = v , nx[cnt] = head[u] , head[u] = cnt++ ; } void DFS(int u,int par) { deg[u] = 1 ; int son = 0 ; for(int i=head[u];~i;i=nx[i]){ int v = to[i] ; if(v==par) continue ; DFS(v,u); deg[u] += deg[v] ; son = max(son,deg[v]); } son = max(son,n-deg[u]); if( son < _son ) res = 0 , _son = son , _node = u , id[res++] = u ; else if(son==_son){ id[res++] = u ; } } void _DFS(int x,int par) { if(deg[x]==1) { node = x ; return ; } for(int i=head[x];~i;i=nx[i]){ int v = to[i] ; if(v==par || v==id[1]) continue ; _DFS(v,x); } } void solve() { for(rd(t);t;t--){ rd(n); memset(head,-1,sizeof(head)); memset(deg,0,sizeof(deg)); for(int i=1,u,v;i<n;i++){ rd(u) , rd(v) ; add(u,v) , add(v,u) ; } _son = inf ; DFS(1,0); if(res==1){ for(int j=1;j<=n;j++){ if(deg[j]==1){ for(int i=head[j];~i;i=nx[i]){ printf("%d %d\n%d %d\n",j,to[i],j,to[i]); break; } break; } } } else { _DFS(id[0],0); for(int i=head[node];~i;i=nx[i]){ printf("%d %d\n%d %d\n",node,to[i],id[1],node); break; } } } } int main() { /// freopen("input.in","r",stdin) ; freopen("output.in","w",stdout) ; ios::sync_with_stdio(false); cin.tie(0) ; cout.tie(0); solve(); }
所遇皆星河