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 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.
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:

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 numbered 1, then the third man would have to give his gift to the second man, but not to the first (a3 = 1). 

链接 http://codeforces.com/contest/681/problem/D

题意

给一棵树,每个子节点与有且仅有一个祖先(包括自己)相匹配,问是否有一个序列,使得每个节点在序列中找到的第一个自己的祖先是自己匹配的节点。

思路

序列存在 等价于 每个节点的匹配等于自己或等于父节点的匹配。
否则若节点p与q匹配,p的父节点为m,m与n匹配,q!=q且q!=n,则此时序列中无论q与n谁在前,p与m总会有一个节点在序列中找到的第一个自己的祖先不是自己的匹配。
用dfs构造序列,对于每棵树,子节点的匹配在前,父节点的匹配在后。不同的树对应的序列间的排列顺序不影响结果。

代码

#include <cstdio>
#include <map>
#include <vector>
using namespace std;
map<int,int>a;
map<int,int>::iterator it,itt;
char s[10];
struct point
{
    char s;
    int num;
}p;
vector<struct point>ans;
int main()
{
    int n,t;
    scanf("%d",&n);
    while (n--)
    {
        scanf("%s",s);
        if (s[0]=='i')
        {
            scanf("%d",&t);
            p.s='i';
            p.num=t;
            ans.push_back(p);
            a[t]++;
        }
        else if (s[0]=='r')
        {
            if (a.empty())
            {
                p.s='i';
                p.num=0;
                ans.push_back(p);
                a[0]++;
            }
            p.s='r';
            p.num=0;
            ans.push_back(p);

            it=a.begin();
            a[it->first]--;
            if (a[it->first]==0)
            {
                a.erase(it->first);             
            }
        }
        else
        {
            scanf("%d",&t);
            if (a.empty())
            {
                p.s='i';
                p.num=t;
                ans.push_back(p);
                a[t]++;
                p.s='g';
                p.num=t;
                ans.push_back(p);
            }
            else
            {
                it=a.begin();
                while (it!=a.end() && it->first<t)
                {
                    p.s='r';
                    p.num=it->first;
                    for (int i=0;i<it->second;i++) ans.push_back(p);
                    itt=it;
                    it++;
                    a.erase(itt->first);
                }
                if (it==a.end() || it->first!=t)
                {
                    p.s='i';
                    p.num=t;
                    ans.push_back(p);
                    a[t]++;
                    p.s='g';
                    p.num=t;
                    ans.push_back(p);
                }
                else
                {

                    p.s='g';
                    p.num=t;
                    ans.push_back(p);
                }
            }
        }
    }
    int l=ans.size();
    printf("%d\n",l);
    for (int i=0;i<l;i++)
    {
        if (ans[i].s=='i') printf("insert %d\n",ans[i].num);
        else if (ans[i].s=='r') printf("removeMin\n");
        else printf("getMin %d\n",ans[i].num);
    }
    return 0;
}

PS

现场naive啊,想的是用并查集处理匹配关系,但是这样之后没法构造序列。。看了眼题解原来直接用dfs就能过。。。
值得总结的是,利用可以人工手算的小数据先模拟一遍过程会有助于理解题意

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值