CF1143C.Queen

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 pip_i. For the root, the value pip_i 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=1c_i = 1, then the vertex ii does not respect any of its ancestors, and if ci=0c_i = 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.

你被给定一棵以 11 到 nn 编号的顶点构成的有根树。树是一种无环连通图。有根树中有一个特殊的顶点,称为根(root)。

顶点 ii 的祖先(ancestors)是指从根到顶点 ii 的路径上除 ii 本身外的所有顶点。顶点 ii 的父节点(parent)是离 ii 最近的祖先。每个顶点都是其父节点的一个子节点(child)。在本题所给的树中,顶点 ii 的父节点为顶点 pip_i;对于根节点,pi=−1p_i = -1。

一棵 n=8n = 8 的树示例,根为顶点 55。顶点 22 的父节点是顶点 33,顶点 11 的父节点是顶点 55。顶点 66 的祖先为顶点 44 和 55;顶点 77 的祖先为顶点 88、33 和 55。

你注意到某些顶点不尊重其他顶点。具体地,若 ci=1c_i = 1,则顶点 ii 不尊重它的任意一个祖先;若 ci=0c_i = 0,则它尊重所有祖先。

你决定逐个删除树中的顶点。每一步中,你选择一个满足如下条件的非根顶点:它不尊重其父节点,且它的任意一个子节点都不尊重它。若存在多个满足条件的顶点,则选择编号最小的那个。当你删除该顶点 vv 时,vv 的所有子节点将直接连接至 vv 的父节点。

删除顶点 77 的示例。

当不再存在满足删除条件的顶点时,该过程停止。请输出顶点被删除的顺序。注意,该顺序是唯一的。

输入格式

The first line contains a single integer nn (1≤n≤1051 \le n \le 10^5) — the number of vertices in the tree.

The next nn lines describe the tree: the ii-th line contains two integers pip_i and cic_i (1≤pi≤n1 \le p_i \le n, 0≤ci≤10 \le c_i \le 1), where pip_i is the parent of the vertex ii, and ci=0c_i = 0, if the vertex ii respects its parents, and ci=1c_i = 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=0c_i=0 for the root. It is guaranteed that the values pip_i define a rooted tree with nn vertices.

第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5)—— 树中顶点的数量。

接下来的 nn 行描述该树:第 ii 行包含两个整数 pip_i 和 cic_i(1≤pi≤n1 \le p_i \le n,0≤ci≤10 \le c_i \le 1),其中 pip_i 是顶点 ii 的父节点,而 ci=0c_i = 0 表示顶点 ii 尊重其父节点,ci=1c_i = 1 表示顶点 ii 不尊重其任意父节点。树的根节点的父节点索引为 −1-1,且根节点满足 ci=0c_i = 0。保证所有 pip_i 的值构成一棵含 nn 个顶点的有根树。

输出格式

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.

如果至少存在一个需要删除的顶点,则输出一行,包含按删除顺序排列的待删除顶点的下标;否则输出单个整数 −1-1。

输入输出样例

  • 输入#1

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

    输出#1

    1 2 4
  • 输入#2

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

    输出#2

    -1
  • 输入#3

    8
    2 1
    -1 0
    1 0
    1 1
    1 1
    4 0
    5 1
    7 0

    输出#3

    5

说明/提示

The deletion process in the first example is as follows (see the picture below, the vertices with ci=1c_i=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:

第一个示例中的删除过程如下(见下图,满足 ci=1c_i=1 的顶点用黄色标出):

  • 首先删除顶点 11,因为它不被其祖先尊重,且其所有子节点(即顶点 22)均不尊重它,同时 11 是满足该条件的编号最小的顶点;
  • 删除后,顶点 22 将与顶点 33 相连;
  • 接着删除顶点 22,因为它不被其祖先尊重,且其所有子节点(仅顶点 44)均不尊重它;
  • 删除后,顶点 44 将与顶点 33 相连;
  • 然后删除顶点 44,因为它不被其祖先尊重,且其所有子节点(不存在)均不尊重它(空真);
  • 于是直接删除顶点 44;
  • 此时已无更多顶点可删除。

在第二个示例中,无需删除任何顶点:

  • 顶点 22 和 33 均拥有尊重它们的子节点;
  • 顶点 44 和 55 均尊重其祖先。

在第三个示例中,树将按如下方式变化:

输入解题思路,AI测评打分。不知道怎么写?

首页