CodeForces - 1143C Queen

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 77are 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.

 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≤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=0for the root. It is guaranteed that the values pipi define a rooted tree with nnvertices.

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

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.

In 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:

题意呢,就是删除所给的节点中 不尊重祖先并且没有儿子节点尊重它的节点。

做的时候没有发现删除节点的独立性,https://blog.csdn.net/weixin_41597684/article/details/88980120

这个老哥的办法比我聪明。

我最后就vector模拟它的删除过程搞出来了。。

代码:

#pragma warning(disable:4996)

#include"stdio.h"
#include"algorithm"
#include"stack"
#include"string.h"
#include"vector"
using namespace std;
int a[100005];
int l[100005], r[100005];
vector<int> v[100005];
int main()
{
	int n;
	while (~scanf("%d", &n))
	{
		memset(l, 0, sizeof(l));
		for (int i = 0; i < n; i++)
		{
			v[i].clear();
		}
		int pl = 0;
		for (int i = 1; i <= n; i++)
		{
			int f, rep;
			scanf("%d %d", &f, &rep);
			l[i] = f;
			v[f].push_back(i);
			r[i] = rep;
			if (rep == 1)
			{
				a[pl++] = i;
			}
		}
		int flag = 1;
		for (int i = 0; i < pl; i++)
		{
			//printf("%d  %d   %d !!\n", a[i], l[a[i]], z[a[i]]);
			if (l[a[i]] == 0)
			{
				continue;
			}
			int h = 0;
			for (int j = 0; j < v[a[i]].size();j++)
			{
				if (r[v[a[i]][j]] == 0)
				{
					h = 1;
				}
			}
			if (h == 0)
			{
				for (int j = 0; j < v[a[i]].size(); j++)
				{
					v[l[a[i]]].push_back(v[a[i]][j]);
					v[a[i]].clear();
				}
				flag = 0;
				printf("%d ", a[i]);
			}
		}
		if (flag)
		{
			printf("-1");
		}
		printf("\n");
	}
	return 0;
}
/*
aabbcabbs
*/
发布了58 篇原创文章 · 获赞 20 · 访问量 5228

猜你喜欢

转载自blog.csdn.net/qq_41658124/article/details/90142944