C. Queen
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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=0ci=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.

Input

The first line contains a single integer nn (1≤n≤1051≤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

 
5
3 1
1 1
-1 0
2 1
3 0
Output

 
1 2 4
Input

 
5
-1 0
1 1
1 1
2 0
3 0
Output

 
-1
Input

 
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 11 , because it does not respect ancestors and all its children (the vertex 22 ) do not respect it, and 11 is the smallest index among such vertices;
  • the vertex 22 will be connected with the vertex 33 after deletion;
  • then you will delete the vertex 22 , because it does not respect ancestors and all its children (the only vertex 44 ) do not respect it;
  • the vertex 44 will be connected with the vertex 33 ;
  • then you will delete the vertex 44 , 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 44 ;
  • there are no more vertices to delete.

n the second example you don't need to delete any vertex:

  • vertices 22 and 33 have children that respect them;
  • vertices 44 and 55 respect ancestors.

In the third example the tree will change this way:

解题思路:

给出一棵树,如果该点被标记为1,而且他所有儿子节点也被标记为1,那么删除这个点,并把儿子节点接到该点父节点上面
代码如下:

 #include<iostream>
#include<vector>
#include<string.h>
using namespace std; int n ;
int tp1 , tp2;
vector<int>a[];
int b[];
int c[];
int main()
{
cin>>n;
memset(b,,sizeof(b));
memset(c,,sizeof(c));
for(int i = ; i <= n; i++)
{
cin>>tp1>>tp2;
if(tp1!=-) //如果它不是根结点;
{
a[tp1].push_back(i); //则放入它的父亲;
}
b[i] = tp2; //并记录tp2,看它自己本身是否尊重父亲;
}
int flag;
for(int i = ; i <=n ;i++)
{
flag = ;
for(int j = ;j < a[i].size() ;j++)
{
if(b[a[i][j]]==)
{
flag = ;
break;
} }
c[i] = flag ; //这个是用来记录它的孩子是否尊重父亲;
}
int flaggg = ;
for(int i = ; i <= n ;i++)
{
if(c[i]==&&b[i]==) //如果它的孩子不尊重父亲并且它自己也不尊重父亲;
{
cout<<i<<" ";
flaggg = ;
}
}
if(flaggg==)
{
cout<<-;
}
return ;
}

最新文章

  1. [troubleshoot][archlinux][X] plasma(KDE) 窗口滚动刷新冻结(约延迟10s)(已解决,root cause不明,无法再次复现)
  2. git Clone SSL certificate problem: self signed certificate
  3. apk反编译(3)smali语法
  4. Understanding transient variables in Java and how they are practically used in HashMap---reference
  5. LinQ学习手册
  6. 从CK+库提取标记点信息
  7. Python一个命令开启http下载服务器(可以局域网内共享文件)
  8. A页面跳转到B页面后打开指定tabs标签
  9. MySQL错误码
  10. 第十二周翻译-《Pro SQL Server Internals, 2nd edition》
  11. css 样式表的书写顺序
  12. iView 的分页结合表格用法
  13. JXL生成Excel,并提供下载(1:生成Excel)
  14. 初始化Weex项目遇到的问题记录
  15. C++ 术语(C++ Primer)
  16. 【剑指offer】斐波那契数列
  17. TCP/UDP client/server library for Java, 最好的java语言tcp udp 服务器客户端实现库
  18. static变量和static函数
  19. loadrunner12-用Chrome如何录制脚本
  20. layui实现checkbox的目录树tree

热门文章

  1. MYSQL的随机查询的实现方法
  2. 批量判断网页是否NOT found
  3. Think In Java 读后感
  4. cocos2d-js反射
  5. 系统性能信息模块之psutil模块
  6. STL中mem_fun, mem_fun_ref用法
  7. [C++ Mind Map] class and memory
  8. tp5 select回显
  9. 4款最受欢迎的Mac原型工具
  10. javascript总结23:javascript 数据类型与变量