$dfs$,后续遍历。

如果某个节点$a[i]=i$,那么$i$的后继的$a[i]$都要指向$i$,直到出现新的后继$j$,$a[j]=j$。利用这个可以判断是否有解。

如果有解的话,那么只要输出后序遍历的结果就可以了。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar(); x = ;
while(!isdigit(c)) c = getchar();
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
} const int maxn=;
int m,n,k,a[maxn],r[maxn];
vector<int>g[maxn];
bool flag[maxn],fail,f[maxn];
vector<int>ans; void dfs(int x,int y)
{
if(a[x]==x) y=x;
else { if(a[x]!=y) fail=; }
for(int i=;i<g[x].size();i++) dfs(g[x][i],y);
if(f[x]) ans.push_back(x);
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int u,v; scanf("%d%d",&u,&v);
g[u].push_back(v); r[v]++;
}
for(int i=;i<=n;i++) { scanf("%d",&a[i]); f[a[i]]=; }
for(int i=;i<=n;i++) if(r[i]==) dfs(i,i);
if(fail) printf("-1\n");
else
{
printf("%d\n",ans.size());
for(int i=;i<ans.size();i++) printf("%d\n",ans[i]);
}
return ;
}

CodeForces 681D Gifts by the List的更多相关文章

  1. codeforces 681D Gifts by the List dfs+构造

    题意:给你一个森林,表示其祖先关系(自己也是自己的祖先),每个人有一个礼物(要送给这个人的固定的一个祖先) 让你构造一个序列,使得的对于每个人,这个序列中第一个出现的他的祖先,是他要送礼物的的那个祖先 ...

  2. CodeForces 681D Gifts by the List (树上DFS)

    题意:一个家庭聚会,每个人都想送出礼物,送礼规则是, 一个人,先看名单列表,发现第一个祖先 就会送给他礼物,然后就不送了,如果他没找到礼物 他会伤心的离开聚会!告诉你m个祖先关系, 和每个人想给谁送! ...

  3. 【33.33%】【codeforces 681D】Gifts by the List

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. Gifts by the List CodeForces - 681D (思维)

    大意: 给定森林, 要求构造一个表, 满足对于每个$x$, 表中第一次出现的$x$的祖先(包括$x$)是$a_x$. 刚开始还想着直接暴力分块优化一下连边, 最后按拓扑序输出... 实际上可以发现$a ...

  5. Codeforces 229E Gifts 概率dp (看题解)

    Gifts 感觉题解写的就是坨不知道什么东西.. 看得这个题解. #include<bits/stdc++.h> #define LL long long #define LD long ...

  6. 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 i ...

  7. Codeforces 755F PolandBall and Gifts bitset + 二进制优化多重背包

    PolandBall and Gifts 转换成置换群后, 对于最大值我们很好处理. 对于最小值, 只跟若干个圈能否刚好组能 k 有关. 最直观的想法就是bitset优化背包, 直接搞肯定T掉. 我们 ...

  8. codeforces 755F F. PolandBall and Gifts(贪心+多重背包)

    题目链接: F. PolandBall and Gifts time limit per test 1.5 seconds memory limit per test 256 megabytes in ...

  9. Codeforces 755 F. PolandBall and Gifts 多重背包+贪心

    F. PolandBall and Gifts   It's Christmas time! PolandBall and his friends will be giving themselves ...

随机推荐

  1. 常用oracle语句-------------------------------------------》(笔记)

      Orale常用语句 1:查询指定表名的字段 select * from sys.user_tab_columns where table_name=表名 //查询指定表名的字段 2: 查询数据库参 ...

  2. 设计模式18---设计模式之策略模式(Strategy)(行为型)

    1.场景模拟 简单的报价管理系统: 对于普通用户和新用户报全价 对于老客户统一折扣5% 对于大客户统一折扣10% 2.不用模式的解决方案 package demo16.strategy.example ...

  3. MongoDB学习之--安全和认证

    MongoDB学习之--安全和认证 本文主要介绍两部分内容,Mongodb的安全检查配置以及安全认证操作: 虽然确保系统安全是系统管理员的重要工作,但是作为程序员了解其机制也是大有好处的,毕竟不是每个 ...

  4. Slide-out Sidebar Menu

    IOS学习之路十(仿人人滑动菜单Slide-out Sidebar Menu) 2013-09-03 22:13 by lixingle, 270 阅读, 0 评论, 收藏, 编辑 最近滑动菜单比较流 ...

  5. addEventListener和attachEvent以及element.onclick的区别

    addEventListener和attachEvent以及element.onclick的区别 attachEvent是ie添加事件处理程序,接收两个参数,其中事件类型名称要加"on&qu ...

  6. jQuery Easing 动画效果扩展

    jQuery API提供了简单的动画效果如淡入淡出以及自定义动画效果,而今天我给大家分享的是一款jQuery动画效果扩展增强插件jquery.easing.js,使用该插件可以实现直线匀速运功.变加速 ...

  7. java正则表达式验证汉字

    统计指定内容的汉字个数: String str = "北京欢迎你 hello welcome!"; int count=0; Pattern pattern = Pattern.c ...

  8. (Java 多线程系列)Java 线程池(Executor)

    线程池简介 线程池是指管理同一组同构工作线程的资源池,线程池是与工作队列(Work Queue)密切相关的,其中在工作队列中保存了所有等待执行的任务.工作线程(Worker Thread)的任务很简单 ...

  9. sqlserver2000 数据库分页查询[根据网上搜索到得sql修改,亲测,可用]

    SELECT TOP 页大小 * FROM TestTable WHERE (ID > (SELECT case when count(0) < 页大小 then 0 else MAX(i ...

  10. MFC控件(7):Split Button

    VS2008中可以看到MFC有一个叫Split Button的控件,要想看它的效果,瞧下QQ那聊天窗口的"发送", "消息记录"这两个按钮就知道了.实际上就是还 ...