【解题报告】Tree Destruction CodeForces - 911F(搜索??)

【解题报告】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:

  1. choose two leaves;
  2. add the length of the simple path between them to the answer;
  3. 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

input

3
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 
#include 
#include 
#include 
#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;iif(!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;iif(!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;iif(!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//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;iscanf("%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(maxlen0;
    }
    vis[p1]=1;
    dis[p1]=0;
    dfs1(p1);
    maxlen=-1;
    for(i=1;i<=n;i++)
    {
        if(maxlen0;
    }
    //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;iprintf("%d %d %d\n",p1,print[i],print[i]);//print记录的是那条直径
    }
    return 0;
}

你可能感兴趣的:(ACM)