C. Queen Codeforces Round #549 (Div. 2) (搜索)

---恢复内容开始---

You are given a rooted tree with vertices numerated from 11 to nn . A tree is a connected graph without cycles. A rooted tree has a special vertex named root.

Ancestors of the vertex ii are all vertices on the path from the root to the vertex ii , except the vertex ii itself. The parent of the vertex ii is the nearest to the vertex ii ancestor of ii . Each vertex is a child of its parent. In the given tree the parent of the vertex ii is the vertex pipi . For the root, the value pipi is 1−1 .

An example of a tree with n=8n=8 , the root is vertex 55 . The parent of the vertex 22 is vertex 33 , the parent of the vertex 11 is vertex 55 . The ancestors of the vertex 66 are vertices 44 and 55 , the ancestors of the vertex 77 are vertices 88 , 33 and 55

You noticed that some vertices do not respect others. In particular, if ci=1ci=1 , then the vertex ii does not respect any of its ancestors, and if ci=0 , it respects all of them.

You decided to delete vertices from the tree one by one. On each step you select such a non-root vertex that it does not respect its parent and none of its children respects it. If there are several such vertices, you select the one with the smallest number. When you delete this vertex vv , all children of vv become connected with the parent of vv .

An example of deletion of the vertex 77 .

Once there are no vertices matching the criteria for deletion, you stop the process. Print the order in which you will delete the vertices. Note that this order is unique.

Input

The first line contains a single integer nn (1n105 ) — the number of vertices in the tree.

The next nn lines describe the tree: the ii -th line contains two integers pipi and cici (1pin1≤pi≤n , 0ci10≤ci≤1 ), where pipi is the parent of the vertex ii , and ci=0ci=0 , if the vertex ii respects its parents, and ci=1ci=1 , if the vertex ii does not respect any of its parents. The root of the tree has 1−1 instead of the parent index, also, ci=0ci=0 for the root. It is guaranteed that the values pipi define a rooted tree with nn vertices.

Output

In case there is at least one vertex to delete, print the only line containing the indices of the vertices you will delete in the order you delete them. Otherwise print a single integer 1−1 .

Examples
Input
Copy
5
3 1
1 1
-1 0
2 1
3 0
Output
Copy
1 2 4 
Input
Copy
5
-1 0
1 1
1 1
2 0
3 0
Output
Copy
-1
Input
Copy
8
2 1
-1 0
1 0
1 1
1 1
4 0
5 1
7 0
Output
Copy
5 
Note

The deletion process in the first example is as follows (see the picture below, the vertices with ci=1ci=1 are in yellow):

  • first you will delete the vertex 1 , because it does not respect ancestors and all its children (the vertex 2 ) do not respect it, and 1 is the smallest index among such vertices;
  • the vertex 2 will be connected with the vertex 3 after deletion;
  • then you will delete the vertex 2 , because it does not respect ancestors and all its children (the only vertex 4 ) do not respect it;
  • the vertex 4 will be connected with the vertex 3 ;
  • then you will delete the vertex 4 , because it does not respect ancestors and all its children (there are none) do not respect it (vacuous truth);
  • you will just delete the vertex 4 ;
  • there are no more vertices to delete.

题意:给你一棵树,每个节点有c值标记,当这个节点的被标记而且它儿子节点都被标记,就删除这个节点,有多个这样的节点就删除优先删除最小的,直到所有都无法删除。

思路:可以bfs直接从上到下删除,也可以dfs从下到上删除,显然删除并不会影响其他该删除的节点

bfs:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int maxn = 1e5+5;
 5 
 6 int n;
 7 int p[maxn],c[maxn];
 8 struct Node
 9 {
10     int to,next;
11     Node(int x=0,int y=0):to(x),next(y){}
12 }node[maxn];
13 int head[maxn];
14 int ans[maxn];
15 int cnt,tot;
16 void add(int x,int y)
17 {
18     node[++cnt].to = y;
19     node[cnt].next = head[x];
20     head[x] = cnt;
21 }
22 
23 void bfs(int s)
24 {
25     queue<int>que;
26     while(!que.empty())que.pop();
27     que.push(s);
28     while(!que.empty())
29     {
30         int t = que.front();
31         que.pop();
32         int k = 1;
33         int id=t;
34         for(int i=head[t];i;i=node[i].next)
35         {
36             int to =node[i].to;
37             if(!c[to])k = 0;
38             que.push(to);
39         }
40         if(k && c[t])ans[++tot] = id;
41     }
42 }
43 int main()
44 {
45     scanf("%d",&n);
46     int s;
47     cnt = tot = 0;
48     for(int i=1;i<=n;i++)
49     {
50         scanf("%d%d",&p[i],&c[i]);
51         if(p[i] == -1)s = i;
52         else add(p[i],i);
53     }
54     bfs(s);
55     sort(ans+1,ans+1+tot);
56     if(!tot)printf("-1\n");
57     else
58     {
59         for(int i=1;i<=tot;i++)
60         {
61             printf("%d ",ans[i]);
62         }
63         puts("");
64     }
65 }
View Code

dfs:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 int n;
 5 const int maxn = 1e5+5;
 6 int head[maxn];
 7 int ans[maxn];
 8 int p[maxn],c[maxn];
 9 struct Node
10 {
11     int to,next;
12     Node(int to=0,int next=0):to(to),next(next){}
13 }node[maxn];
14 int cnt,tot;
15 void add(int x,int y)
16 {
17     node[++cnt].to = y;
18     node[cnt].next = head[x];
19     head[x] = cnt;
20 }
21 
22 void dfs(int s,bool turn)
23 {
24     int k=1;
25     for(int i=head[s];i;i=node[i].next)
26     {
27         int to = node[i].to;
28         if(!c[to])k=0;
29         dfs(to,c[to]);
30     }
31     if(k && turn)ans[++tot] = s;
32 }
33 
34 int main()
35 {
36     scanf("%d",&n);
37     int s;
38     cnt = tot = 0;
39     for(int i=1;i<=n;i++)
40     {
41         scanf("%d%d",&p[i],&c[i]);
42         if(p[i] == -1)s=i;
43         else add(p[i],i);
44     }
45     dfs(s,0);
46     sort(ans+1,ans+tot+1);
47     if(tot)
48     for(int i=1;i<=tot;i++)printf("%d ",ans[i]);
49     else printf("-1\n");
50 }
View Code

猜你喜欢

转载自www.cnblogs.com/iwannabe/p/10687142.html