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 ;
}

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

  1. [题解] Codeforces Round #549 (Div. 2) B. Nirvana

    Codeforces Round #549 (Div. 2) B. Nirvana [题目描述] B. Nirvana time limit per test1 second memory limit ...

  2. Codeforces Round #549 (Div. 1)

    今天试图用typora写题解 真开心 参考 你会发现有很多都是参考的..zblzbl Codeforces Round #549 (Div. 1) 最近脑子不行啦 需要cf来缓解一下 A. The B ...

  3. C. Queen Codeforces Round #549 (Div. 2) dfs

    C. Queen time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

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

    ---恢复内容开始--- You are given a rooted tree with vertices numerated from 11 to nn . A tree is a connect ...

  5. Codeforces Round #549 (Div. 2) 训练实录 (5/6)

    The Doors +0 找出输入的01数列里,0或者1先出完的的下标. Nirvana +3 输入n,求1到n的数字,哪个数逐位相乘的积最大,输出最大积. 思路是按位比较,从低到高,依次把小位换成全 ...

  6. CodeForces Round #549 Div.2

    A. The Doors 代码: #include <bits/stdc++.h> using namespace std; ; int N; , One = ; int a[maxn], ...

  7. [ Codeforces Round #549 (Div. 2)][D. The Beatles][exgcd]

    https://codeforces.com/contest/1143/problem/D D. The Beatles time limit per test 1 second memory lim ...

  8. Codeforces Round #549 (Div. 2) Solution

    传送门 A.The Doors 看懂题目就会写的题 给一个 $01$ 序列,找到最早的位置使得 $0$ 或 $1$ 已经全部出现 #include<iostream> #include&l ...

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

    https://codeforces.com/contest/1143/problem/F 题意 有n条形如\(y=x^2+bx+c\)的抛物线,问有多少条抛物线上方没有其他抛物线的交点 题解 \(y ...

随机推荐

  1. Android——eclipse共享library以及导出jar包[转]

    目录(?)[-] 一apk之间共享Class 二apk导出jar包   android的apk在在eclipse上进行开发的时候,有时候需要import其它包中的一些class,正常的方法就是在jav ...

  2. haproxy 配置 说明

    一.环境说明实验环境OS CentOS5.4192.168.0.14    proxy192.168.0.24    web1192.168.0.64    web2 官方地址:http://hapr ...

  3. Unity strip engine code 遇到執行不能之問題與解決

    遊戲發布在 WebGL 平台發現檔案還是太大,因此在 IL2CPP 的環境下,開啟 Strip engine code 編譯功能,嘗試看看能不能減少一些檔案容量. 但由於我們另外有載入 Scene s ...

  4. 天云CloudStack 改进版

    整体风格   创建区域

  5. RTX二次开发笔记2

    问题一:关于DLL文件的引用 在安装文件夹内 APIObject.dll==>RTXSAPI.dll 服务器API接口 RTXCAPI.DLL ==> 客户端API接口 问题二:RTX二次 ...

  6. 服务器监控方案CactiEZ V10.1

    CactiEZ中文版是最简单有效的Cacti中文解决方案,整合Spine,RRDTool和美化字体.集成Thold,Monitor,Syslog,Weathermap,Realtime,Errorim ...

  7. [C++] 2D Array's memory allocation

    2D Array's memory allocation

  8. QT学习之事件处理

    Qt事件机制 Qt程序是事件驱动的, 程序的每个动作都是由幕后某个事件所触发.. Qt事件的发生和处理成为程序运行的主线,存在于程序整个生命周期. Qt事件的类型很多, 常见的qt的事件如下: 键盘事 ...

  9. 网站配置新的YII2可运行版本

    1. config 中的 gitignore 先将 -local 加入版本库 2. 将 runtime 目录添加写权限 3. 将web目录的asset 添加写的权限 4.

  10. SpringMVC错误集中营

    1.eclipse里的错误提示为The import javax.servlet.http.HttpServletRequest cannot be resolved 1.这是因为工程里面web-in ...