Codeforces 681D Gifts by the List【思维+Dfs】
来源:互联网 发布:淘宝自动提取卡密 编辑:程序博客网 时间:2024/04/28 04:24
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 aren men in Sasha's family, so let's number them with integers from1 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 numberB;
- there is a man number C, such that the man numberA 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 numberB and A ≠ B, then the man numberB 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 manB 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 then 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 n andm (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 integerspi andqi (1 ≤ pi, qi ≤ n,pi ≠ qi) meaning that the man numberedpi is the father of the man numberedqi. 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 numberedi wants to give a gift to the man numberedai. It is guaranteed that for every1 ≤ i ≤ n the man numbered ai is an ancestor of the man numberedi.
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 exceedingn — 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.
3 21 22 31 2 1
-1
4 21 23 41 2 3 3
3213
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(a1 = a3 = 1);
- if there would be no 2 in the list then the second man wish would not be satisfied(a2 = 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(a2 = 2).
- if, at the other hand, the man numbered 2 would stay before the man numbered1, then the third man would have to give his gift to the second man, but not to the first(a3 = 1).
给你N个点,M条边,u,v表示u是v的祖先。
并且已知每个人想送给编号为x的祖先一个礼物。
现在让你构造出来一个序列,使得每个人从上到下看到的第一个祖先,就把礼物送给他,试满足所有人最终送的礼物都送给了自己想送的人身上。
思路:
对于一个点u来讲,假设这个点作为根出现,那么很显然,其所有的子节点,要么want【v】=u.要么want【v】=v.否则这个点v是送不到自己想送的人身上的。
那么我们不妨Dfs处理,动态维护一下就行。
Ac代码:
#include<stdio.h>#include<string.h>#include<vector>using namespace std;vector<int >mp[200040];int want[200040];int degree[200040];int ans[200040];int f[200040];int flag,cnt;void Dfs(int u,int x){ for(int i=0;i<mp[u].size();i++) { int v=mp[u][i]; Dfs(v,x); if(want[v]!=v&&want[v]!=want[u]) { flag=0; } } if(want[u]==u) ans[cnt++]=u;}int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { memset(f,0,sizeof(f)); memset(degree,0,sizeof(degree)); for(int i=1;i<=n;i++)mp[i].clear(); for(int i=0;i<m;i++) { int x,y; scanf("%d%d",&x,&y); mp[x].push_back(y); degree[y]++; } cnt=0; for(int i=1;i<=n;i++)scanf("%d",&want[i]); flag=1; for(int i=1;i<=n;i++) { if(degree[i]==0)Dfs(i,i); } if(flag==0) { printf("-1\n"); } else { printf("%d\n",cnt); for(int i=0;i<cnt;i++) { printf("%d\n",ans[i]); } printf("\n"); } }}
- Codeforces 681D Gifts by the List【思维+Dfs】
- Codeforces 681D Gifts by the List dfs+构造
- 【33.33%】【codeforces 681D】Gifts by the List
- Codeforces Round #357 (Div. 2) D Gifts by the List(DFS)
- Codeforces Round #357 (Div. 2) -- D. Gifts by the List(DFS)
- Codeforces Round #357 (Div. 2) -- D. Gifts by the List(DFS)
- Codeforces Round #357 (Div. 2) D. Gifts by the List
- Codeforces Round #357 (Div. 2)-D. Gifts by the List
- Codeforces Round #357 (Div. 2)D:Gifts by the List
- Codeforces Round #357 (Div. 2) D. Gifts by the List
- [Codeforces Round #357 DIV2D (CF681D)] Gifts by the List
- Codeforces 711D Directed Roads【Dfs+思维】
- Codeforces 525D Arthur and Walls 【Dfs+思维】
- DFS(思维) CodeForces - 525D Arthur and Walls
- codeforces 395 D <思维,DFS想了半天超时>
- Codeforces 618D Hamiltonian Spanning Tree【思维+Dfs】
- Codeforces 813C The Tag Game【思维+Dfs】
- Codeforces 733D D. Kostya the Sculptor By Assassin
- 通信方式——NFC概述(一)
- 给出文本框一 和二 自动计算文本框三的值
- lmbench基本的部署与使用
- 29.进程的管理
- Swift3.0中如何完成不同View Controller之间的切换
- Codeforces 681D Gifts by the List【思维+Dfs】
- 读分布式服务框架原理与实践读后感
- 泛型
- MATLAB1.3 图片题作业
- jquery-ul-li实现分页功能 转载仅供交流
- STC串口回传ID号,及波特率
- python3.5小爬虫—爬糗事百科
- java 异常处理的一些特殊情况
- 基于 vue2 + vuex 构建一个具有 45 个页面的大型单页面应用