【解题报告】Tree Destruction CodeForces - 911F(搜索??)
2018-01-08 17:21
483 查看
【解题报告】Tree Destruction CodeForces - 911F(搜索??)
====================================================================【cf链接】
【vj链接】
======================================================================
题目
You are given an unweighted tree with n vertices. Then 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
Obviously after 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!
Input
The first line contains one integer number n (2 ≤ n ≤ 2·105) — the number of vertices in the tree.Next n - 1 lines describe the edges of the tree in form ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi). It is guaranteed that given graph is a tree.
Output
In the first line print one integer number — maximal possible answer.In the next n - 1 lines print the operations in order of their applying in format ai, bi, ci, where ai, bi — pair of the leaves that are chosen in the current operation (1 ≤ ai, bi ≤ n), ci (1 ≤ ci ≤ n, ci = ai or ci = bi) — choosen leaf that is removed from the tree in the current operation.
See the examples for better understanding.
Examples
input3 1 2 1 3
output
3 2 3 3 2 1 1
input
5 1 2 1 3 2 4 2 5
output
9 3 5 5 4 3 3 4 1 1 4 2 2
====================================================================
题目大意
有棵树,按照某规律删除结点并得到一个值。某规律:找俩点,删掉其中一个
一个值:每次操作找的那俩点的距离
样例输入
第一行:一个n,表示树上有几个点第2-n行:两个数x,y,表示一条边
输出
第一行:刚刚说的那一个值第2-n行:三个数x,y,z,x,y表示一条边,z表示删除的那个点
====================================================================
解题思路
如果已经有一条路径(直径)是最长的,那其他点的最长路径一定是到达直径的端点的假设这两个点叫p1,p2
要求的一个值叫ans
步骤一:找到最长路径(两次dfs)
(从一个点出发,找到它的最长路径,那另一个点一定属于直径的端点)
步骤二:除最长路径上的点全部删掉
步骤三:删掉直径
(步骤二三通过分别从p1,p2遍历得到长度dis[i],dis2[i],用d[i]表示在直径上的某点离p2的距离,然后遍历一次,回溯时删除)
(求ans的过程在遍历之前,直接加d[i]或dis[i]或dis2[i]即可)
===================================================================
AC代码
不建议看这个代码,太乱了,幸好是1A,不然都不知道怎么改……#include <iostream> #include <cstdio> #include <algorithm> #include <vector> #define LL long long #define maxn 200007 using namespace std; vector <int> edge[maxn]; int dis[maxn],vis[maxn],d[maxn],p1,p2,dis2[maxn],print[maxn];//print是方便输出的,不影响做题 void dfs1(int x)//求x到各点的距离存在dis数组里 { for(int i=0;i<edge[x].size();i++) { if(!vis[edge[x][i]]) { vis[edge[x][i]]=1; dis[edge[x][i]]=dis[x]+1; dfs1(edge[x][i]); } } } void dfs3(int x)//求x到各点的距离存在dis2数组里 { for(int i=0;i<edge[x].size();i++) { if(!vis[edge[x][i]]) { vis[edge[x][i]]=1; dis2[edge[x][i]]=dis2[x]+1; dfs3(edge[x][i]); } } } int dfs2(int x)//求x到直径上各点的距离,存在d数组里 { if(x==p2) return d[x]=1; for(int i=0;i<edge[x].size();i++) { if(!vis[edge[x][i]]) { vis[edge[x][i]]=1; if(dfs2(edge[x][i])) { d[x]=d[edge[x][i]]+1; return 1; } } } return 0; } int dfs4(int x)//输出除直径外的点 {//printf("A"); for(int i=0;i<edge[x].size();i++) {//printf("B"); if(!vis[edge[x][i]]) { vis[edge[x][i]]=1; dfs4(edge[x][i]); if(d[edge[x][i]]==0) {//printf("D"); if(dis[edge[x][i]]>=dis2[edge[x][i]]) printf("%d %d %d\n",p1,edge[x][i],edge[x][i]); else printf("%d %d %d\n",p2,edge[x][i],edge[x][i]); } } } } int main() { int n,i,j,x,y,maxlen; scanf("%d",&n); for(i=1;i<n;i++) { scanf("%d%d",&x,&y); edge[x].push_back(y); edge[y].push_back(x); } vis[1]=1; dis[1]=0; dfs1(1); maxlen=-1; for(i=1;i<=n;i++) { if(maxlen<dis[i]) { maxlen=dis[i]; p1=i; } vis[i]=0; } vis[p1]=1; dis[p1]=0; dfs1(p1); maxlen=-1; for(i=1;i<=n;i++) { if(maxlen<dis[i]) { maxlen=dis[i]; p2=i; } vis[i]=0; } //printf("p1=%d p2=%d \n",p1,p2); dfs2(p1); dis2[p2]=0; for(i=1;i<=n;i++) vis[i]=0; vis[p2]=1; dfs3(p2); LL ans=0; for(i=1;i<=n;i++) { //printf("%d %d %d\n",dis[i],dis2[i],d[i]); if(!d[i]) ans+=max(dis[i],dis2[i]); else ans+=d[i]-1,print[d[i]]=i; } printf("%lld\n",ans); for(i=1;i<=n;i++) vis[i]=0; vis[p1]=1; dfs4(p1); for(i=1;i<d[p1];i++) { printf("%d %d %d\n",p1,print[i],print[i]);//print记录的是那条直径 } return 0; }
相关文章推荐
- codeforces_229_div2解题报告
- codeforces 485A.Factory 解题报告
- BZOJ 3106 [cqoi 2013] 对抗搜索 解题报告
- [codeforces解题报告]More Reclamation
- codeforces 344 B. Print Check解题报告
- NOIP2015 斗地主 解题报告(搜索)
- POJ1979 /Openjudge1818 Red and Black解题报告(深度优先搜索,图的遍历)
- 2018.1.30【 CodeForces - 620C 】解题报告(STL,set容器)
- 2018.2.1【 CodeForces - 527C 】解题报告(STL,set,边长和位置的维护)
- 解题报告 noi 2005 智慧珠游戏(BT 搜索)
- FOJ--1046--Tempter of the Bone--解题报告(典型的迷宫搜索)
- Codeforces 459(#261 (Div. 2) ) 解题报告
- 搜索题目推荐及解题报告
- 解题报告 之 CodeForces 91B Queue
- UOJ147 [NOIP2015]斗地主 解题报告【搜索】【贪心】
- Codeforces 193D Two Segments 解题报告
- codeforces A. Flipping Game 解题报告
- 洛谷 1441 砝码称重 搜索+DP 解题报告
- 2018.1.29【 CodeForces - 455A 】解题报告(dp,long long)
- codeforces解题报告与总结