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.

规定一个顺序,使分为父子结点,则每次删除只能往子节点查找

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
#define ll long long
vector<int>v[],ans;
stack<int>q;
int cnt[],vis[];
int n,x,pos,par[];
void bfs(int now)
{
ans.push_back(now);
vis[now]=;
for(int i=;i<v[now].size();i++)
{
cnt[v[now][i]]--;
if(v[now][i]==par[now] || vis[v[now][i]]) continue;//当前节点已经被找过,或者是now节点的父节点
if(!(cnt[v[now][i]]&)) bfs(v[now][i]);
}
}
void dfs(int fa,int now)
{
par[now]=fa;
q.push(now);
for(int i=;i<v[now].size();i++)
{
if(v[now][i]==fa) continue;
dfs(now,v[now][i]);
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&x);
if(x)
{
v[i].push_back(x);
v[x].push_back(i);
cnt[i]++;
cnt[x]++;
}
else pos=i;
}
dfs(,pos);//n-1条边,则必有一个为0,不妨把这点作为根节点遍历。
memset(vis,,sizeof(vis));
while(!q.empty())
{
int fr=q.top();
q.pop();
if(!(cnt[fr]&)) bfs(fr);//从后向前遍历,若存在,必只有一个结点符合初始为偶数
}
if(ans.size()==n)
{
printf("YES\n");
for(int i=;i<ans.size();i++)
printf("%d\n",ans[i]);
}
else printf("NO\n");
return ;
}

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

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

  3. Codeforces963B - Destruction of a Tree

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

  4. leetcode算法: Find Bottom Left Tree Value

    leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...

  5. LeetCode第[98]题(Java):Validate Binary Search Tree(验证二叉搜索树)

    题目:验证二叉搜索树 难度:Medium 题目内容: Given a binary tree, determine if it is a valid binary search tree (BST). ...

  6. 二叉树系列 - 二叉搜索树 - [LeetCode] 中序遍历中利用 pre节点避免额外空间。题:Recover Binary Search Tree,Validate Binary Search Tree

    二叉搜索树是常用的概念,它的定义如下: The left subtree of a node contains only nodes with keys less than the node's ke ...

  7. 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 解 对于题目要求,显然一开始的树,要求度数为偶数的节点个数为奇数个,通过奇 ...

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

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

  9. [LeetCode]题解(python):110 Balanced Binary Tree

    题目来源 https://leetcode.com/problems/balanced-binary-tree/ Given a binary tree, determine if it is hei ...

随机推荐

  1. CSDN 轻松周赛赛题:能否被8整除

    轻松周赛赛题:能否被8整除 题目详情 给定一个非负整数,问能否重排它的全部数字,使得重排后的数能被8整除. 输入格式: 多组数据,每组数据是一个非负整数.非负整数的位数不超过10000位. 输出格式 ...

  2. R语言学习(一)前言

    本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/49768161 R是一个有着统计分析功能 ...

  3. QUERY_REWRITE_ENABLED

    官方文档中: QUERY_REWRITE_ENABLED Property Description Parameter type String Syntax QUERY_REWRITE_ENABLED ...

  4. maven3+eclipse搭建webAPP企业级实战《一》

    想做企业级web系统:环境搭建不可缺少GO 1:新建 2:next : 3:选择webAPP next: 填完finish 初始项目结构: watermark/2/text/aHR0cDovL2Jsb ...

  5. nj07---npm

    一.如何使用包管理器 Node.js包管理器,即npm是Node.js官方提供的包管理工具,它已经成了Node.js包的标准发布平台,用于Node.js包的发布.传播.依赖控制.(可以下载上面的包也可 ...

  6. hpuoj--校赛--面试难题(区间相交问题)

    问题 F: 感恩节KK专场--面试难题 时间限制: 1 Sec  内存限制: 128 MB 提交: 294  解决: 39 [提交][状态][讨论版] 题目描述 有n个人要来面试学生会XX部门,要求面 ...

  7. C# Hook

    C# Hook原理及EasyHook简易教程 前言 在说C# Hook之前,我们先来说说什么是Hook技术.相信大家都接触过外挂,不管是修改游戏客户端的也好,盗取密码的也罢,它们都是如何实现的呢? 实 ...

  8. 设计url 通过分发的方式 Xadmin_demo

    如 urlpatterns = [ url(r'^Xadmin/',([ url(r'^add/$', views.add) url(r'^delete/$', views.delete) ], No ...

  9. Autofac依赖注入框架

    最近使用Autofac框架做项目的依赖注入,感觉挺好用的. 没有深入研究,只是拿来用用,具体可以去官网看看:https://autofac.org/. 这里只是贴一下最近项目的配置: public p ...

  10. webi和universe

    Universe是一个包含以下内容的文件: 1 一个或多个数据库中间件的连接参数. 2 称为对象的SQL结构,映射到数据库中的实际SQL结构,如列,表和数据库函数.其中对象是按类分组的.用户既可以看到 ...