Codeforces Round #357 (Div. 2) D. Gifts by the List 水题
D. Gifts by the List
题目连接:
http://www.codeforces.com/contest/681/problem/D
Description
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.
A list of candidates is prepared, containing some (possibly all) of the n men in some order.
Each of the n men decides to give a gift.
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.
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 ≤ n, pi ≠ 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.
Sample Input
3 2
1 2
2 3
1 2 1
Sample Output
-1
Hint
题意
给你一个森林
然后给你每个人想要给谁礼物的意愿
这些人给礼物是根据一个列表,他们会从上往下看,看到第一个是他祖先的人,就会给这个人礼物
你需要构造一个列表,满足所有人的意愿。
题解:
直接dfs
如果这个人的意愿不是他自己,或者这个人的意愿和他的父亲不同的话,那么就输出无解,因为这样必然会和他的父亲发生冲突。
为什么呢?
如果a要给b礼物,那么a和b之间的所有人,都必须给b礼物。
答案的话,按照dfs顺序倒序输出就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
int n,m,d[maxn],key[maxn],a[maxn],flag;
vector<int> E[maxn],ans;
void dfs(int x,int p)
{
for(int v=0;v<E[x].size();v++)
{
int u=E[x][v];
if(u==p)continue;
if(a[u]!=a[x]&&a[u]!=u)flag=1;
dfs(u,x);
}
if(a[x]==x)ans.push_back(x);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int a,b;scanf("%d%d",&a,&b);
E[a].push_back(b);
E[b].push_back(a);
d[b]++;
}
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
if(!d[i])dfs(i,0);
if(flag)
{
cout<<"-1"<<endl;
return 0;
}
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();i++)
cout<<ans[i]<<endl;
}
Codeforces Round #357 (Div. 2) D. Gifts by the List 水题的更多相关文章
- Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)
Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...
- Codeforces Round #373 (Div. 2) C. Efim and Strange Grade 水题
C. Efim and Strange Grade 题目连接: http://codeforces.com/contest/719/problem/C Description Efim just re ...
- Codeforces Round #185 (Div. 2) A. Whose sentence is it? 水题
A. Whose sentence is it? Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...
- Codeforces Round #373 (Div. 2) A. Vitya in the Countryside 水题
A. Vitya in the Countryside 题目连接: http://codeforces.com/contest/719/problem/A Description Every summ ...
- Codeforces Round #371 (Div. 2) A. Meeting of Old Friends 水题
A. Meeting of Old Friends 题目连接: http://codeforces.com/contest/714/problem/A Description Today an out ...
- Codeforces Round #355 (Div. 2) B. Vanya and Food Processor 水题
B. Vanya and Food Processor 题目连接: http://www.codeforces.com/contest/677/problem/B Description Vanya ...
- Codeforces Round #310 (Div. 2) B. Case of Fake Numbers 水题
B. Case of Fake Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- Codeforces Round #309 (Div. 2) B. Ohana Cleans Up 字符串水题
B. Ohana Cleans Up Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/554/pr ...
- Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks 字符串水题
A. Kyoya and Photobooks Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
随机推荐
- select()函数用法三之poll函数
poll是Linux中的字符设备驱动中有一个函数,Linux 2.5.44版本后被epoll取代,作用是把当前的文件指针挂到等待队列,和select实现功能差不多. poll()函数:这个函数是某些U ...
- python高性能web框架——Japronto
近期做了一个简单的demo需求,搭建一个http server,支持简单的qa查询.库中有10000个qa对,需要支持每秒10000次以上的查询请求. 需求比较简单,主要难点就是10000+的RPS. ...
- 【小程序开发】上拉加载更多demo
wxml: <scroll-view class='swiper-scroll' scroll-y="{{true}}" bindscrolltolower="lo ...
- delphi TComponent类 2
来自:http://blog.csdn.net/lailai186/article/details/7442385 ------------------------------------------ ...
- 关于Fuzz——peach的学习
最近在搞漏洞挖掘,之前写过一个文件格式漏洞挖掘的博文,使用的是光刃牛写的Alpha Fuzz工具.感觉样本生成的质量不是很好,这次打算使用一下老牌的Fuzz工具peach.学长介绍了一下说peach的 ...
- 更新svn的客户端TortoiseSVN后 ,之前使用svn管理的文件的关联图标消失了
说明:下面的解决方法及图片来自博客:装了SVN,你的关联图标变了没有? 解决办法:在同步的文件点击右键如下图 ... 现则Settings,出现的界面如下 ... ...
- 《精通Python设计模式》学习结构型之代理模式
这种模式,总会让人想到SRPING中的AOP, 不同语言有不同的实现方式吧. class SensitiveInfo: def __init__(self): self.users = ['nick' ...
- Vue.js学习笔记(一) - 起步
本篇将简单介绍一下Vue.js,并在Node.js环境下搭建一个简单的Demo. 一.简介 我个人理解,Vue.js是一套前端视图层的框架,它只关心视图展示和数据绑定,它的一些语法与Angular 1 ...
- Linux 的软件安装目录
Linux 的软件安装目录是也是有讲究的,理解这一点,在对系统管理是有益的 /usr:系统级的目录,可以理解为C:/Windows/,/usr/lib理解为C:/Windows/System32. / ...
- tomcat中请求参数中文中乱码问题
在server.xml中配置如下: <Connector connectionTimeout="20000" port="8080" protocol=& ...