B. Destruction of a Tree
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a tree (a graph with n vertices and n - 1 edges in which it's possible to reach any vertex from any other vertex using only its edges).

A vertex can be destroyed if this vertex has even degree. If you destroy a vertex, all edges connected to it are also deleted.

Destroy all vertices in the given tree or determine that it is impossible.

Input

The first line contains integer n (1 ≤ n ≤ 2·105) — number of vertices in a tree.

The second line contains n integers p1, p2, ..., pn (0 ≤ pi ≤ n). If pi ≠ 0 there is an edge between vertices i and pi. It is guaranteed that the given graph is a tree.

Output

If it's possible to destroy all vertices, print "YES" (without quotes), otherwise print "NO" (without quotes).

If it's possible to destroy all vertices, in the next n lines print the indices of the vertices in order you destroy them. If there are multiple correct answers, print any.

Examples
input

Copy
5
0 1 2 1 2
output

Copy
YES
1
2
3
5
4
input

Copy
4
0 1 2 3
output

Copy
NO
Note

In the first example at first you have to remove the vertex with index 1 (after that, the edges (1, 2) and (1, 4) are removed), then the vertex with index 2 (and edges (2, 3) and (2, 5) are removed). After that there are no edges in the tree, so you can remove remaining vertices in any order.

题意:

给出一棵树,每次可以删除一个度数为偶的点 和 与这个点相连的边。

问是否存在一种方案吧整个树都删除,如果存在,输出任意方案。

题解:

分析:叶节点一定是不能直接删掉的(度数为1),因此要想删掉叶节点,必须先删掉其父节点。

如果父节点的度数为偶,则可以将其删去,

如果父节点的度数为奇,那么要想删掉该父节点,必须先删掉此父节点的父节点………………

是不是有一点向根递归的感觉。

刚才遗漏了很多细节,现在来完善这个思路:

设一个节点的子节点中,度数为奇的子节点数量为cnt[0],度数为偶的子节点数量为cnt[1]

这个节点的度数deg=cnt[0]+cnt[1]+1

度数为偶的子节点要首先删掉的,那么该节点剩下的度数就是deg-cnt[1]

如果(deg-cnt[1])%2==0,那么这个点也可以直接删掉。

如果(deg-cnt[1])%2==1,那么这个点不能直接删掉,需要先删掉其父节点。

那么一个有意思的树形dp就出来了,v[i]表示将 i 的子节点中能直接删掉的删掉后,i 能否直接删掉(v[i]==0表示不能,v[i]==1表示能)。

最后看v[root]是否为1就可以判断yes,no。

输出方案:

对于一个节点,先删除其子节点中能直接删除的,然后将这个节点删除,这样其不能删除的子节点就可以删除了,将其删除。

递归处理,输出即可。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int read(){
int xx=,ff=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')ff=-;ch=getchar();}
while(ch>=''&&ch<=''){xx=xx*+ch-'';ch=getchar();}
return xx*ff;
}
const int maxn=;
int N,root;
int lin[maxn],len,deg[maxn];
struct edge{
int y,next;
}e[maxn<<];
inline void insert(int xx,int yy){
e[++len].next=lin[xx];
lin[xx]=len;
e[len].y=yy;
deg[xx]++;
}
bool v[maxn];//v==1:can delete now,v==0:can not delete now
int cnt[maxn][];
bool dfs(int x,int fa){
for(int i=lin[x];i;i=e[i].next)
if(e[i].y!=fa)
cnt[x][dfs(e[i].y,x)]++;
if((deg[x]-cnt[x][])%==)
v[x]=;
else
v[x]=;
return v[x];
}
void print(int x,int fa){
for(int i=lin[x];i;i=e[i].next)
if(e[i].y!=fa)
if(v[e[i].y])
print(e[i].y,x);
printf("%d\n",x);
for(int i=lin[x];i;i=e[i].next)
if(e[i].y!=fa)
if(!v[e[i].y])
print(e[i].y,x);
}
int main(){
//freopen("in.txt","r",stdin);
N=read();
for(int i=;i<=N;i++){
int temp=read();
if(!temp)
root=i;
else
insert(i,temp),insert(temp,i);
}
if(dfs(root,)){
puts("YES");
print(root,);
}
else
puts("NO");
return ;
}

codeforces 963B Destruction of a Tree的更多相关文章

  1. CodeForces - 963B Destruction of a Tree (dfs+思维题)

    B. Destruction of a Tree time limit per test 1 second memory limit per test 256 megabytes input stan ...

  2. Codeforces 963B Destruction of a Tree 思维+dfs

    题目大意: 给出一棵树,每次只能摧毁有偶数个度的节点,摧毁该节点后所有该节点连着的边都摧毁,判断一棵树能否被摧毁,若能,按顺序输出摧毁的点,如果有多种顺序,输出一种即可 基本思路: 1)我一开始自然而 ...

  3. codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)

    codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...

  4. codeforces 812E Sagheer and Apple Tree(思维、nim博弈)

    codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...

  5. codeforces 220 C. Game on Tree

    题目链接 codeforces 220 C. Game on Tree 题解 对于 1节点一定要选的 发现对于每个节点,被覆盖切选中其节点的概率为祖先个数分之一,也就是深度分之一 代码 #includ ...

  6. Codeforces963B - Destruction of a Tree

    Portal Description 给出一个\(n(n\leq2\times10^5)\)个点的树,每次可以删除一个度数为偶数的点及其相连的边,求一种能够删掉整棵树的方案. Solution 简单起 ...

  7. Codeforces E. Alyona and a tree(二分树上差分)

    题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  8. Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1) 963B 964D B Destruction of a Tree

    题 OvO http://codeforces.com/contest/963/problem/B CF 963B 964D 解 对于题目要求,显然一开始的树,要求度数为偶数的节点个数为奇数个,通过奇 ...

  9. 963B:Destruction of a Tree

    You are given a tree (a graph with n vertices and n - 1 edges in which it's possible to reach any ve ...

随机推荐

  1. 使用ajax解析后台json数据时:Unexpected token o in JSON at position 1

    json数据解析异常 今天在做json数据的时候,出现了如下错误,说是解析异常. VM1584:1 Uncaught SyntaxError: Unexpected token o in JSON a ...

  2. [Python3网络爬虫开发实战] 3.1-使用urllib

    在Python 2中,有urllib和urllib2两个库来实现请求的发送.而在Python 3中,已经不存在urllib2这个库了,统一为urllib,其官方文档链接为:https://docs.p ...

  3. centos 简单用户管理

    一.配置文件 /etc/passwd:存放用户信息,以“:”分割成7个部分 1.账号名称,用来对应UID: 2.早期密码存放位置,后来密码改存/etc/shadow中,以“x”代替: 3.UID,使用 ...

  4. 81-Gator Oscillator,加多摆动指标.(2015.7.1)

    Gator Oscillator 加多摆动指标 Oscillator,加多摆动指标.(2015.7.1)" title="81-Gator Oscillator,加多摆动指标.(2 ...

  5. Eclipse调试相关

    Eclipse调试相关 F5 step into就是单步执行,遇到子函数就进入并且继续单步执行. F6 step over是在单步执行时,在函数内遇到子函数时不会进入子函数内单步执行,而是将子函数整个 ...

  6. Ubuntu12.04之修改密码

    Ubuntu 12.04 默认root没有密码 修改密码方式如下: test@localhost:~$ sudo passwd root [sudo] password for test: 输入新的 ...

  7. Codeforces 631C Report【其他】

    题意: 给定序列,将前a个数进行逆序或正序排列,多次操作后,求最终得到的序列. 分析: 仔细分析可以想到j<i,且rj小于ri的操作是没有意义的,对于每个i把类似j的操作删去(这里可以用mult ...

  8. [bzoj1510][POI2006]Kra-The Disks_暴力

    Kra-The Disks bzoj-1510 POI-2006 题目大意:题目链接. 注释:略. 想法:不难发现其实只有前缀最小值是有效的. 进而我们把盘子一个一个往里放,弄一个自底向上的指针往上蹦 ...

  9. BZOJ——2190: [SDOI2008]仪仗队

    思路: 我们将其所在的位置设为(0,0),那么如果存在一个点(x,y),且有gcd(x,y)=k(k!=1),那么点(x/k,y/k)一定会将(x,y)挡住.而如果k=1,那么点(x,y)就一定会被看 ...

  10. Divide Two Integers(模拟计算机除法)

    Divide two integers without using multiplication, division and mod operator. 由于不能用乘号,除号,和取余.那么一个数除另外 ...