Codeforces Round #357 (Div. 2)D:Gifts by the List

D. Gifts by the List
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 nmen 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 ofA 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?

Input

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 pi and qi (1 ≤ pi, qi ≤ npi ≠ qi) meaning that the man numbered pi is the father of the man numbered qi. 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 a1, a2, ..., an (1 ≤ ai ≤ n), ith of which means that the man numbered i wants to give a gift to the man numbered ai. It is guaranteed that for every 1 ≤ i ≤ n the man numbered ai is an ancestor of the man numbered i.

Output

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.

Examples
input
3 2
1 2
2 3
1 2 1
output
-1
input
4 2
1 2
3 4
1 2 3 3
output
3
2
1
3

题意:
直接说样例,先输入两个数n和m表示一个家族共有n个人,其中有m对关系,之后输入这m对关系ai,bi表示ai是bi的父亲,
最后输入一排共n个数,第i个数ki表示第i个人想给第ki个人送礼(可以给自己送礼),然后问你是否存在这样一个名单,保证每个人从上往下看名单遇到的第一个满足是自己的祖宗或自己的人正好是自己想要送礼的那个人,如果有从上到下输出这个名单,否则输出-1(答案可能不唯一)

题解:
推理下,如果存在这样的名单一定满足一个条件:对于任意一对父子, 父亲和儿子必须想给同一个人送礼,不然就无解,除非儿子是想送给自己,然而假设父亲和儿子都想把礼物送给C,那么 C一定是他们的祖宗且C只能将礼物送给自己
给个例子:c是b的父亲,b是a 的父亲,那么当b想给c送礼,a就只能给自己送礼或也给c送礼,前者答案为a c,后者答案为c


#include<stdio.h>
#include<vector>
using namespace std;
vector<int> v[100005], ans;
int flag = 1, like[100005], in[100005] = {0};
void Sech(int x);
int main(void)
{
	int n, i, m, a, b;
	scanf("%d%d", &n, &m);
	for(i=1;i<=m;i++)
	{
		scanf("%d%d", &a, &b);
		v[a].push_back(b);
		in[b]++;
	}
	for(i=1;i<=n;i++)
		scanf("%d", &like[i]);
	for(i=1;i<=n;i++)
	{
		if(in[i]==0)
			Sech(i);
	}
	if(flag==0)
		printf("-1\n");
	else
	{
		printf("%d\n", ans.size());
		for(i=0;i<ans.size();i++)
			printf("%d\n", ans[i]);
	}
	return 0;
}

void Sech(int x)
{
	int i, a;
	for(i=0;i<v[x].size();i++)
	{
		a = v[x][i];
		if(like[a]!=a && like[a]!=like[x])
			flag = 0;
		Sech(a);
	}
	if(like[x]==x)
		ans.push_back(x);
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值