CF963B Destruction of a Tree(树+构造)

给定一棵树,每次删掉一个偶数度数的点及他的边,问能否把这棵树全删掉。
可以证明偶数个点的树一定不行。(每次一定是删掉偶数条边,而偶数个点的树有奇数条边。)
奇数个点的树一定可以。为啥呢,你只要会证就好啦。
证明具体参见Visjiao 博客:portal

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 200010
inline char gc(){
    static char buf[1<<16],*S,*T;
    if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
    return x*f;
}
int n,sz[N],du[N],h[N],num=0,rt;
struct edge{
    int to,next;
}data[N<<1];
void dfs(int x,int Fa){
    sz[x]=1;
    for(int i=h[x];i;i=data[i].next){
        int y=data[i].to;if(y==Fa) continue;
        dfs(y,x);sz[x]+=sz[y];
    }
}
void dfs1(int x,int Fa){
    for(int i=h[x];i;i=data[i].next){
        int y=data[i].to;if(y==Fa) continue;
        if(!(sz[y]&1)) dfs1(y,x);
    }printf("%d\n",x);
    for(int i=h[x];i;i=data[i].next){
        int y=data[i].to;if(y==Fa) continue;
        if(sz[y]&1) dfs1(y,x);
    }
}
int main(){
//  freopen("a.in","r",stdin);
    n=read();if(!(n&1)){puts("NO");return 0;}puts("YES");
    for(int x=1;x<=n;++x){
        int y=read();if(!y) continue;du[x]++;du[y]++;
        data[++num].to=y;data[num].next=h[x];h[x]=num;
        data[++num].to=x;data[num].next=h[y];h[y]=num;
    }for(int i=1;i<=n;++i) if(!(du[i]&1)){rt=i;break;}
    dfs(rt,0);dfs1(rt,0);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/icefox_zhx/article/details/80415908