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 (1≤n≤105 ) — the number of vertices in the tree.

The next nn lines describe the tree: the ii -th line contains two integers pipi and cici (1≤pi≤n1≤pi≤n , 0≤ci≤10≤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 }

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 }

原文地址:https://www.cnblogs.com/iwannabe/p/10687142.html

时间: 2024-10-05 05:50:33

C. Queen Codeforces Round #549 (Div. 2) (搜索)的相关文章

Codeforces Round #549 (Div. 1)

今天试图用typora写题解 真开心 参考 你会发现有很多都是参考的..zblzbl Codeforces Round #549 (Div. 1) 最近脑子不行啦 需要cf来缓解一下 A. The Beatles 这道题就是枚举啦 有两种步长 试一下就好了 如果你的步长是x 那么要跳的次数就是距离除以步长 \[ \frac{n * k * x}{gcd(n * k, x)} \div x = \frac{n * k}{gcd(n * k, x)} \] #include <cmath> #in

Codeforces Round #549 (Div. 2)

A.The Doors 记录最后一个0和1的位置. B.Nirvana 对于每一位,答案有三种情况: 1,取这位原本数字; 2,取x-1,同时让后一位取9; 3,让前面全取9; C.Queen 一个点如果会被删,那么其他的点被删不会影响它最后被删的结果,判断一下那些点会被删, 然后排序. D.The Beatles 显然\( L = c \times k + a - b \) 或 \( L = c \times k - a - b \),对于每个L,\[ step = lcm(n \times

Codeforces Round #549 (Div. 2) D 数学

https://codeforces.com/contest/1143/problem/D 题意 有nk个城市,第1,k+1,2k+1,...,(n-1)k+1城市有餐厅,你每次能走l距离,a为起始位置离最近餐厅的距离,b为走了一次后离最近餐厅的距离,给出n,k,a,b,求你回到起点最少和最多停留次数 题解 \(yl=xnk,有y=xnk/l,即y=lcm(xnk,l)/l\) 枚举a(两种情况),b(两种情况),维护最大,最小值 代码 #include<bits/stdc++.h> #def

Codeforces Round #549 (Div. 2) F 数形结合 + 凸包(新坑)

https://codeforces.com/contest/1143/problem/F 题意 有n条形如\(y=x^2+bx+c\)的抛物线,问有多少条抛物线上方没有其他抛物线的交点 题解 \(y=x^2+bx+c=>y+x^2=bx+c\),转换为点\((x,y+x^2)\)在bx+c的直线上 两个点确定一条抛物线,同时也确定了一条直线 需要选择最上面那些点相邻确定的抛物线,所以维护一个上凸包即可 维护上凸包,当前点在前进方向左边需要向后退,cross(a,b)>=0 代码 #inclu

B. Nirvana Codeforces Round #549 (Div. 2) (递归dfs)

---恢复内容开始--- Kurt reaches nirvana when he finds the product of all the digits of some positive integer. Greater value of the product makes the nirvana deeper. Help Kurt find the maximum possible product of digits among all integers from 1 to n. Input

Codeforces Round #224 (Div. 2) D 暴力搜索加记忆化

题意读了半年,英语太渣,题意是摆两个棋子在棋盘上作为起点,但是起点不能在#上,然后按照图的指示开始走, < 左 > 右 ^上 v下,走的时候只能按照图的指示走,如果前方是 #的话,可以走进去,但是 走进去之后便不能再走了,走的途中两个棋子不能相碰,但是最终都走到同一个#里是没事的,并且若是能走 无限步的话 输出 -1, 例如  > < 这样左右左右的走就能无限走,然后问你 两个棋子走的最大步数的和 一开始被输出-1给困住了,因为除了 .> <这样以外  还可以刚好形成一

Codeforces Round #244 (Div. 2)

A. Police Recruits B. Prison Transfer A,B两个是水题. C. Checkposts DFS找出所有的环就行了. 每次搜索一个结点u时,给u加一个递增标号low[u],同时记录搜索u及u的子结点过程中遇到的最小标号minc,也就是当搜索u的子结点v时,minc = min(minc, low[v]).搜索完成后,如果minc < low[u],说明搜索u的子结点时又回到了u的父结点,也就是说u在一个环中,然后求出这个环的最小费用及取到最小费用的结点数. D.

Codeforces Round #261 (Div. 2)[ABCDE]

Codeforces Round #261 (Div. 2)[ABCDE] ACM 题目地址:Codeforces Round #261 (Div. 2) A - Pashmak and Garden 题意: 一个正方形,它的边平行于坐标轴,给出这个正方形的两个点,求出另外两个点. 分析: 推断下是否平行X轴或平行Y轴,各种if. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: A.cpp * Create Date: 2014-0

BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls

题目传送门 1 /* 2 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 3 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 4 在2*2的方格里,若只有一个是'*',那么它一定要被替换掉 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 #include <queue> 1