思路:留意到一个性质,如果a想送礼物的对象不是自己或者不是和祖先一样的话那么就可以直接判-1,因为如果a想送给b的话那么必须a到b中间所有都送给b。证明就是比如a->b->c->d,如果a想送给d,b想送给c的话,显然就不成立了。然后就是dfs一波就可以了
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
int a[maxn];
vector<int>e[maxn],ans;
int d[maxn],flag;
void dfs(int u,int fa)
{
for(int i = 0;i<e[u].size();i++)
{
int v = e[u][i];
if(v==fa)
continue;
if(a[v]!=a[u]&&a[v]!=v)
flag=1;
dfs(v,u);
}
if(a[u]==u)
ans.push_back(u);
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i = 1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
e[u].push_back(v);
e[v].push_back(u);
d[v]++;
}
for(int i = 1;i<=n;i++)
scanf("%d",&a[i]);
for(int i = 1;i<=n;i++)
if(!d[i])
dfs(i,-1);
if (flag)
{
cout << "-1" << endl;
return 0;
}
cout << ans.size() << endl;
for(int i = 0;i<ans.size();i++)
cout << ans[i] << endl;
}
D. Gifts by the List
time limit per test
memory limit per test
input
output
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.
A is considered to be the ancestor of the man number B
- A = B;
- A is the father of the man number B;
- C, such that the man number A is his ancestor and the man number C is the father of the man number B.
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.
- n
- n
- A looks through the list and picks the first man B in the list, such that B is an ancestor of A
- If there is no ancestor of a person in the list, he becomes sad and leaves the celebration without giving a gift to anyone.
n
Input
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.
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.
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
k (1 ≤ k ≤ n)
k pairwise different positive integers not exceeding n
- 1
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
Note
The first sample explanation:
- 1 in the list then the first and the third man's wishes would not be satisfied (a1 = a3;
- 2 in the list then the second man wish would not be satisfied (a2;
- 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 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 (a3