CF911F Tree Destruction
Posted five20
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF911F Tree Destruction相关的知识,希望对你有一定的参考价值。
题意翻译
给你一棵树,每次挑选这棵树的两个叶子,加上他们之间的边数(距离),然后将其中一个点去掉,问你边数(距离)之和最大可以是多少.
题目描述
You are given an unweighted tree with n n n vertices. Then n−1 n-1 n−1 following operations are applied to the tree. A single operation consists of the following steps:
- choose two leaves;
- add the length of the simple path between them to the answer;
- remove one of the chosen leaves from the tree.
Initial answer (before applying operations) is 0 0 0 . Obviously after n−1 n-1 n−1 such operations the tree will consist of a single vertex.
Calculate the maximal possible answer you can achieve, and construct a sequence of operations that allows you to achieve this answer!
输入输出格式
输入格式:The first line contains one integer number n n n ( 2<=n<=2⋅105 2<=n<=2·10^{5} 2<=n<=2⋅105 ) — the number of vertices in the tree.
Next n−1 n-1 n−1 lines describe the edges of the tree in form ai,bi a_{i},b_{i} ai?,bi? ( 1<=ai 1<=a_{i} 1<=ai? , bi<=n b_{i}<=n bi?<=n , ai≠bi a_{i}≠b_{i} ai?≠bi? ). It is guaranteed that given graph is a tree.
输出格式:In the first line print one integer number — maximal possible answer.
In the next n−1 n-1 n−1 lines print the operations in order of their applying in format ai,bi,ci a_{i},b_{i},c_{i} ai?,bi?,ci? , where ai,bi a_{i},b_{i} ai?,bi? — pair of the leaves that are chosen in the current operation ( 1<=ai 1<=a_{i} 1<=ai? , bi<=n b_{i}<=n bi?<=n ), ci c_{i} ci? ( 1<=ci<=n 1<=c_{i}<=n 1<=ci?<=n , ci=ai c_{i}=a_{i} ci?=ai? or ci=bi c_{i}=b_{i} ci?=bi? ) — choosen leaf that is removed from the tree in the current operation.
See the examples for better understanding.
输入输出样例
3
1 2
1 3
3
2 3 3
2 1 1
5
1 2
1 3
2 4
2 5
9
3 5 5
4 3 3
4 1 1
4 2 2
Solution:
贪心+树的直径。
dfs处理出树的直径,然后先删去非直径上的分枝的叶子节点,再删直径。
树的直径一定是树上最长的一条简单路径,然后每次删掉非直径上的叶子节点的最远距离,一定是该节点与直径两端点中的某一个搭配出的最长距离,证明就一句话,不可能存在两个都是非直径上的叶子节点的路径超过直径的长度,画图或者脑补,其实蛮简单的贪心,就是难想到。
具体实现时,两遍dfs处理出直径,再dfs一遍处理出各节点到直径两端的距离,直径上的节点对答案的总贡献直接等差数列求和,非直径上的点直接累加到直径两段距离的最大值,然后输出方案,我的做法是将非直径上的节点和直径两端点的搭配都压入堆中,以距离为关键字,每次弹出就输出方案,最后只剩直径时就是固定一端依次删另一端就好了。
代码:
1 #include<bits/stdc++.h> 2 #define il inline 3 #define ll long long 4 #define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++) 5 #define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--) 6 using namespace std; 7 const int N=4e5+7; 8 int n,to[N],net[N],h[N],cnt; 9 int tp[N],q[N],tot,maxn; 10 ll dis1[N],dis2[N],ans; 11 bool vis[N]; 12 struct node{ 13 int u,v; 14 ll d; 15 node(int a=0,int b=0,ll c=0){u=a;v=b;d=c;} 16 bool operator<(const node &a)const {return d<a.d;} 17 }; 18 priority_queue<node>Q; 19 20 il int gi(){ 21 int a=0;char x=getchar(); 22 while(x<‘0‘||x>‘9‘)x=getchar(); 23 while(x>=‘0‘&&x<=‘9‘)a=(a<<3)+(a<<1)+x-48,x=getchar(); 24 return a; 25 } 26 27 il void add(int u,int v){to[++cnt]=v,net[cnt]=h[u],h[u]=cnt;} 28 29 il void dfs1(int u,int lst){ 30 for(int i=h[u];i;i=net[i]) 31 if(!vis[to[i]]) vis[to[i]]=1,dfs1(to[i],lst+1),vis[to[i]]=0; 32 if(lst>maxn) q[tot=1]=u,maxn=lst; 33 } 34 35 il void dfs2(int u,int lst){ 36 for(int i=h[u];i;i=net[i]) 37 if(!vis[to[i]]) tp[lst]=to[i],vis[to[i]]=1,dfs2(to[i],lst+1),vis[to[i]]=0; 38 if(lst>tot) { 39 tot=lst; 40 For(i,2,lst) q[i]=tp[i]; 41 } 42 } 43 44 il void dfs3(int u,ll *a){ 45 for(int i=h[u];i;i=net[i]) 46 if(!vis[to[i]]) a[to[i]]=a[u]+1,vis[to[i]]=1,dfs3(to[i],a),vis[to[i]]=0; 47 } 48 49 int main(){ 50 n=gi(); 51 int u,v; 52 For(i,1,n-1) u=gi(),v=gi(),add(u,v),add(v,u); 53 vis[1]=1,dfs1(1,0),tot=0,vis[1]=0,vis[q[1]]=1,dfs2(q[1],2),tot--; 54 dfs3(q[1],dis1),vis[q[1]]=0,vis[q[tot]]=1,dfs3(q[tot],dis2); 55 For(i,1,tot) vis[q[i]]=1; 56 ans=1ll*tot*(tot-1)/2; 57 For(i,1,n) 58 if(!vis[i])ans+=max(dis1[i],dis2[i]),Q.push(node(q[1],i,dis1[i])),Q.push(node(q[tot],i,dis2[i])); 59 printf("%lld ",ans); 60 while(!Q.empty()){ 61 node a=Q.top();Q.pop(); 62 if(!vis[a.v]) printf("%d %d %d ",a.u,a.v,a.v),vis[a.v]=1; 63 } 64 Bor(i,2,tot) printf("%d %d %d ",q[1],q[i],q[i]); 65 return 0; 66 }
以上是关于CF911F Tree Destruction的主要内容,如果未能解决你的问题,请参考以下文章