CodeForces 964D Destruction of a Tree (删除树上偶度点 dfs序的巧用)

题目:http://codeforces.com/contest/964/problem/D

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

5
0 1 2 1 2

Output

YES
1
2
3
5
4

Input

4
0 1 2 3

Output

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.

【题意】

给出一棵树,现在我可以删除度为偶数的点,问最后能否删除所有点?

【分析】

优先考虑与叶子节点相邻的结点,若为偶数度,则删除。以这种方式贪心。

求出dfs序,按其逆序开始考虑每一个点,就能做到,每次考虑的都是叶子节点(在每一步都做了删除点的操作的情况下)

【代码】

#include<bits/stdc++.h>
using namespace std;

int p[202020];
int du[202020];
int parent[202020];
bool vis[202020];
vector<int>G[202020];
vector<int>vec,ans;
void dfsxu(int u,int fa=-1)
{
    vec.push_back(u);
    parent[u]=fa;
    for(int i=0;i<G[u].size();i++)
        if(G[u][i]!=fa)dfsxu(G[u][i],u);
}
void dfsdel(int u)
{
    ans.push_back(u);
    vis[u]=1;
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        du[v]--;
        if(v==parent[u]||vis[v]||du[v]%2)continue;
        dfsdel(v);
    }
}
int main()
{
    int n;
    ios::sync_with_stdio(0);
    cin>>n;
    memset(du,0,sizeof(du));
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
    {
        cin>>p[i];
        if(p[i])
        {
            G[i].push_back(p[i]);
            G[p[i]].push_back(i);
            du[i]++;
            du[p[i]]++;
        }
    }
    dfsxu(1);
    for(int i=vec.size()-1;i>=0;i--)
    {
        int v=vec[i];
        if(du[v]%2==0)
            dfsdel(v);
    }

    if(ans.size()<n)cout<<"NO"<<endl;
    else
    {
        cout<<"YES\n";
        for(int i=0;i<ans.size();i++)
            cout<<ans[i]<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/winter2121/article/details/81089553