cf963b Destruction of a Tree

越靠近叶子越优先删掉

#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
int n, uu, hea[200005], cnt, deg[200005], fa[200005];
bool vis[200005];
vector<int> shu;
vector<int> ans;
struct Edge{
    int too, nxt;
}edge[400005];
void add_edge(int fro, int too){
    edge[++cnt].nxt = hea[fro];
    edge[cnt].too = too;
    hea[fro] = cnt;
}
void dfs(int x, int f){
    shu.push_back(x);
    fa[x] = f;
    for(int i=hea[x]; i; i=edge[i].nxt){
        int t=edge[i].too;
        if(t!=f)    dfs(t, x);
    }
}
void shanchu(int x){
    vis[x] = true;
    ans.push_back(x);
    for(int i=hea[x]; i; i=edge[i].nxt){
        int t=edge[i].too;
        deg[t]--;
        if(t!=fa[x] && !vis[t]){
            if(deg[t]%2==0)
                shanchu(t);
        }
    }
}
int main(){
    cin>>n;
    for(int i=1; i<=n; i++){
        scanf("%d", &uu);
        if(!uu) continue;
        add_edge(uu, i);
        add_edge(i, uu);
        deg[i]++; deg[uu]++;
    }
    dfs(1, 0);
    for(int i=shu.size()-1; i>=0; i--){
        int x=shu[i];
        if(/*vis[x] ||*/deg[x]&1)   continue;
        shanchu(x);
    }
    if(ans.size()!=n)   printf("NO\n");
    else{
        printf("YES\n");
        for(auto i:ans) printf("%d\n", i);
    }
    return 0;
}

转载于:https://www.cnblogs.com/poorpool/p/8899069.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值