CF681D.Gifts by the List

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Sasha lives in a big happy family. At the Man's Day all the men of the family gather to celebrate it following their own traditions. There are n men in Sasha's family, so let's number them with integers from 1 to n.

Each man has at most one father but may have arbitrary number of sons.

Man number A is considered to be the ancestor of the man number B if at least one of the following conditions is satisfied:

  • A = B;
  • the man number A is the father of the man number B;
  • there is a man number C, such that the man number A is his ancestor and the man number C is the father of the man number B.

Of course, if the man number A is an ancestor of the man number B and A ≠ B, then the man number B is not an ancestor of the man number A.

The tradition of the Sasha's family is to give gifts at the Man's Day. Because giving gifts in a normal way is boring, each year the following happens.

  1. A list of candidates is prepared, containing some (possibly all) of the n men in some order.
  2. Each of the n men decides to give a gift.
  3. In order to choose a person to give a gift to, man A looks through the list and picks the first man B in the list, such that B is an ancestor of A and gives him a gift. Note that according to definition it may happen that a person gives a gift to himself.
  4. If there is no ancestor of a person in the list, he becomes sad and leaves the celebration without giving a gift to anyone.

This year you have decided to help in organizing celebration and asked each of the n men, who do they want to give presents to (this person is chosen only among ancestors). Are you able to make a list of candidates, such that all the wishes will be satisfied if they give gifts according to the process described above?

萨沙生活在一个幸福的大家庭中。在“男士节”这一天,家族中所有的男性都会聚集在一起,按照自家的传统庆祝节日。萨沙家共有 nn 位男性,我们用从 11 到 nn 的整数为他们编号。

每位男性最多有一位父亲,但可以有任意数量的儿子。

若满足以下任一条件,则称编号为 AA 的男性是编号为 BB 的男性的祖先:

  • A=BA = B;
  • 编号为 AA 的男性是编号为 BB 的男性的父亲;
  • 存在某位编号为 CC 的男性,使得 AA 是 CC 的祖先,且 CC 是 BB 的父亲。

显然,若 AA 是 BB 的祖先且 A≠BA \neq B,则 BB 不可能是 AA 的祖先。

萨沙家族的传统是在“男士节”互赠礼物。由于以普通方式送礼过于乏味,每年都会按如下方式举行:

  1. 准备一份候选人名单,其中包含 nn 位男性中的某些(可能全部)人,按某种顺序排列;
  2. 每位男性均决定送出一份礼物;
  3. 为选定收礼人,编号为 AA 的男性会依次浏览该名单,选出名单中第一位满足“是 AA 的祖先”的男性 BB,并将礼物赠予他。注意:根据上述定义,某人完全可能将礼物送给自己;
  4. 若名单中不存在 AA 的任何祖先,则 AA 会感到伤心,并离开庆祝活动,不向任何人赠送礼物。

今年,你决定协助组织这次庆祝活动,于是询问了这 nn 位男性每人希望将礼物送给谁(仅限于自己的祖先)。你能否构造出一份候选人名单,使得当所有人严格依照上述流程送礼时,所有人的愿望都能被满足?

输入格式

In the first line of the input two integers n and m (0 ≤ m < n ≤ 100 000) are given — the number of the men in the Sasha's family and the number of family relations in it respectively.

The next m lines describe family relations: the (i + 1)th line consists of pair of integers p__i and q__i (1 ≤ p__i, q__i ≤ n, p__i ≠ q__i) meaning that the man numbered p__i is the father of the man numbered q__i. It is guaranteed that every pair of numbers appears at most once, that among every pair of two different men at least one of them is not an ancestor of another and that every man has at most one father.

The next line contains n integers _a_1, _a_2, ..., a__n (1 ≤ a__i ≤ n), i__th of which means that the man numbered i wants to give a gift to the man numbered a__i. It is guaranteed that for every 1 ≤ i ≤ n the man numbered a__i is an ancestor of the man numbered i.

输入的第一行包含两个整数 nn 和 mm(0 ≤ m < n ≤ 100 0000 ≤ m < n ≤ 100\,000),分别表示 Sasha 家族中男性成员的数量以及家族关系的数量。

接下来的 mm 行描述家族关系:第 (i+1)(i+1) 行包含一对整数 pip_i 和 qiq_i(1 ≤ pi, qi ≤ n1 ≤ p_i,\,q_i ≤ n,且 pi ≠ qip_i ≠ q_i),表示编号为 pip_i 的男性是编号为 qiq_i 的男性的父亲。保证每对数字至多出现一次,任意两个不同男性之间,至少有一人不是另一人的祖先,且每个男性至多有一个父亲。

下一行包含 nn 个整数 a1, a2, ..., ana_1,\,a_2,\,...,\,a_n(1 ≤ ai ≤ n1 ≤ a_i ≤ n),其中第 ii 个数 aia_i 表示编号为 ii 的男性希望将礼物送给编号为 aia_i 的男性。保证对每个 1 ≤ i ≤ n1 ≤ i ≤ n,编号为 aia_i 的男性是编号为 ii 的男性的祖先。

输出格式

Print an integer k (1 ≤ k ≤ n) — the number of the men in the list of candidates, in the first line.

Print then k pairwise different positive integers not exceeding n — the numbers of the men in the list in an order satisfying every of the men's wishes, one per line.

If there are more than one appropriate lists, print any of them. If there is no appropriate list print  - 1 in the only line.

在第一行输出一个整数 kk(1 ≤ k ≤ n1 \le k \le n)—— 候选人名单中男士的数量。

然后在接下来的 kk 行中,每行输出一个不超过 nn 的互不相同的正整数—— 满足每位男士愿望的候选人名单中的男士编号(按满足愿望的顺序)。

若存在多个符合条件的名单,输出任意一个即可。若不存在符合条件的名单,则仅在唯一的一行中输出 −1-1。

输入输出样例

  • 输入#1

    3 2
    1 2
    2 3
    1 2 1

    输出#1

    -1
  • 输入#2

    4 2
    1 2
    3 4
    1 2 3 3

    输出#2

    3
    2
    1
    3

说明/提示

The first sample explanation:

  • if there would be no 1 in the list then the first and the third man's wishes would not be satisfied (_a_1 = _a_3 = 1);
  • if there would be no 2 in the list then the second man wish would not be satisfied (_a_2 = 2);
  • if 1 would stay before 2 in the answer then the second man would have to give his gift to the first man, but he wants to give it to himself (_a_2 = 2).
  • if, at the other hand, the man numbered 2 would stay before the man numbered 1, then the third man would have to give his gift to the second man, but not to the first (_a_3 = 1).

第一个样例解释:

  • 如果列表中没有数字 1,则第一个人和第三个人的愿望将无法满足(a1=a3=1a_1 = a_3 = 1);
  • 如果列表中没有数字 2,则第二个人的愿望将无法满足(a2=2a_2 = 2);
  • 如果在答案中 1 出现在 2 之前,则第二个人必须将自己的礼物送给第一人,但他希望将礼物送给自己(a2=2a_2 = 2);
  • 另一方面,如果编号为 2 的人排在编号为 1 的人之前,则第三人必须将自己的礼物送给第二人,而非第一人(a3=1a_3 = 1)。

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

首页