codeforce911F(树的直径)

emmmm这题思路有点奇特。。然后实现起来还有很多坑点。。比起那些思路一般实现困难的题更讨人喜欢→_→

显然要先找出树的直径。。然后找出来后先处理直径以外的点。。最远距离肯定是和最远点的距离,然后删完之后直径就可以随便弄了。。。

实现的过程有个坑点就是。。得从叶子往里删。。所以在删的时候要找到其中一个最远点的距离进行排序。。这样就能确保往里删了。。。然后还爆int。。

代码由于推掉重写了几次。。所以并不是特别好看。。



#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define inf 1e9
#define eps 1e-8
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define NM 200005
#define nm 800005
#define pi 3.141592653
using namespace std;
int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}

struct edge{int t;edge*next;}e[nm],*h[NM],*o=e;
void add(int x,int y){o->t=y;o->next=h[x];h[x]=o++;}
int n,_x,_y,_t,__t,tmp[NM],d[NM],b[NM],tot,f[NM],c[NM];
ll ans;
bool v[NM];
bool cmp(int x,int y){return d[x]>d[y];}
void bfs(int u){
    queue<int >q;
    mem(d);mem(v);
    inc(i,1,n)d[i]=inf;
    d[u]=0;v[u]++;q.push(u);
    while(!q.empty()){
        int t=q.front();q.pop();v[t]=false;
        link(t)if(d[j->t]>d[t]+1){
            d[j->t]=d[t]+1;
            if(!v[j->t])q.push(j->t),v[j->t]++;
        }
    }
}

void dfs(int x){
    b[++tot]=x;v[x]++;
    if(x==_y)
    inc(i,1,tot)c[i]=b[i];
    link(x)if(!v[j->t])dfs(j->t);
    tot--;v[x]=false;
}
int main(){
    n=read();
    inc(i,1,n-1){_x=read();_y=read();add(_x,_y);add(_y,_x);}
    bfs(1);
    inc(i,1,n)if(d[i]>d[_x])_x=i;
    bfs(_x);
    inc(i,1,n)f[i]=d[i];
    inc(i,1,n)if(d[i]>d[_y])_y=i;
    bfs(_y);
    mem(v);
    dfs(_x);
    //inc(i,1,n)printf("%d ",c[i]);
    inc(i,1,n+1)if(c[i])b[i]=c[i];else{tot=i-1;break;}
    //printf("%d %d\n",_x,_y);
    //inc(i,1,tot)printf("%d ",b[i]);
    inc(i,1,tot)v[b[i]]++;
    inc(i,1,n)if(v[i])ans+=d[i];
    else ans+=max(d[i],f[i]);
    printf("%I64d\n",ans);
    inc(i,1,n)tmp[i]=i;
	sort(tmp+1,tmp+1+n,cmp);
	for(int k=1,i=tmp[k];k<n;i=tmp[++k])if(!v[i])
		if(f[i]>d[i])printf("%d %d %d\n",_x,i,i);
		else printf("%d %d %d\n",_y,i,i);
	for(int k=1,i=tmp[k];k<n;i=tmp[++k])if(v[i])
		printf("%d %d %d\n",_y,i,i);
	return 0;
}


F. Tree Destruction
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 answer (before applying operations) is 0. 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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值