CF681D.Gifts by the List

普及/提高-

通过率:0%

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 nn men in Sasha's family, so let's number them with integers from 11 to nn .

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

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

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

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

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 nn men in some order.
  2. Each of the nn men decides to give a gift.
  3. In order to choose a person to give a gift to, man AA looks through the list and picks the first man BB in the list, such that BB is an ancestor of AA 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 nn 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?

输入格式

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

The next mm lines describe family relations: the (i+1)th(i+1)^{th} line consists of pair of integers pip_{i} and qiq_{i} ( 1<=pi,qi<=n1<=p_{i},q_{i}<=n , piqip_{i}≠q_{i} ) meaning that the man numbered pip_{i} is the father of the man numbered qiq_{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 nn integers a1,a2,...,ana_{1},a_{2},...,a_{n} ( 1<=ai<=n1<=a_{i}<=n ), ithi^{th} of which means that the man numbered ii wants to give a gift to the man numbered aia_{i} . It is guaranteed that for every 1<=i<=n1<=i<=n the man numbered aia_{i} is an ancestor of the man numbered ii .

输出格式

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

Print then kk pairwise different positive integers not exceeding nn — 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-1 in the only line.

输入输出样例

  • 输入#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 11 in the list then the first and the third man's wishes would not be satisfied (a1=a3=1)(a_{1}=a_{3}=1) ;
  • if there would be no 22 in the list then the second man wish would not be satisfied (a2=2)(a_{2}=2) ;
  • if 11 would stay before 22 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 (a2=2)(a_{2}=2) .
  • if, at the other hand, the man numbered 22 would stay before the man numbered 11 , then the third man would have to give his gift to the second man, but not to the first (a3=1)(a_{3}=1) .
首页