题意:给你一个森林,表示其祖先关系(自己也是自己的祖先),每个人有一个礼物(要送给这个人的固定的一个祖先)

让你构造一个序列,使得的对于每个人,这个序列中第一个出现的他的祖先,是他要送礼物的的那个祖先

分析:这个序列满足

1:我们这个序列只需要出现那些被送礼物的人就好了

2:这个序列的元素保证,如果x是y的祖先,那么x在y的后面

具体:这个序列可以通过dfs构造,然后最后check一下这个序列

注:check的过程和构造序列的dfs过程同步

#include <cstdio>
#include <iostream>
#include <ctime>
#include <vector>
#include <cmath>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=1e5+;
const int INF=0x3f3f3f3f;
const int mod=1e9+;
struct Edge{
int v,next;
}edge[N];
int head[N],tot,d[N],n,m,a[N],ret[N];
void add(int u,int v){
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
}
stack<int>s;
bool vis[N];
void dfs(int u,int fir){
if(vis[u])fir=u,s.push(u);
ret[u]=fir;
for(int i=head[u];~i;i=edge[i].next)
dfs(edge[i].v,fir);
}
int main(){
scanf("%d%d",&n,&m);
memset(head,-,sizeof(head));
for(int i=;i<m;++i){
int u,v;
scanf("%d%d",&u,&v);
++d[v];add(u,v);
}
for(int i=;i<=n;++i){
scanf("%d",&a[i]);
vis[a[i]]=true;
}
for(int i=;i<=n;++i)
if(!d[i])dfs(i,i);
for(int i=;i<=n;++i)
if(ret[i]!=a[i]){printf("-1\n");return ;}
printf("%d\n",s.size());
while(!s.empty()){
printf("%d\n",s.top());
s.pop();
}
return ;
}

codeforces 681D Gifts by the List dfs+构造的更多相关文章

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

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

  2. Codeforces 946 课程表背包DP 数位DFS构造

    A B 给你A,B 两个数      1.a=0 OR b=0 break      2.a>=2b a=a-2b        3.b>=2a b=b-2a 如果只是单纯模拟肯定会超时 ...

  3. CodeForces 681D Gifts by the List

    $dfs$,后续遍历. 如果某个节点$a[i]=i$,那么$i$的后继的$a[i]$都要指向$i$,直到出现新的后继$j$,$a[j]=j$.利用这个可以判断是否有解. 如果有解的话,那么只要输出后序 ...

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

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

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

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

  6. codeforces 1282 E. The Cake Is a Lie (dfs+构造)

    链接:https://codeforces.com/contest/1282/problem/E 题意:给的是一张平面图,是一个n边形,每次可以切一刀,切出一个三角形,最终切成n-2个三角形.题目给出 ...

  7. codeforces 359E Neatness(DFS+构造)

    Simon loves neatness. So before he goes to bed, Simon wants to complete all chores in the house. Sim ...

  8. Codeforces 653B Bear and Compressing【DFS】

    题目链接: http://codeforces.com/problemset/problem/653/B 题意: 要求你构造一个长度为n的字符串使得通过使用m个操作,最终获得字符a.已知第i个操作将字 ...

  9. Codeforces 1163E Magical Permutation [线性基,构造]

    codeforces 思路 我顺着图论的标签点进去的,却没想到-- 可以发现排列内每一个数都是集合里的数异或出来的. 考虑答案的上界是多少.如果能用小于\(2^k\)的数构造出\([0,2^k-1]\ ...

随机推荐

  1. 一个很好的php分词类库

    PHPAnalysis源程序下载与演示: PHP分词系统 V2.0 版下载 | PHP分词系统演示 | PHPAnalysis类API文档   原文连接地址:http://www.phpbone.co ...

  2. 毕向东JAVA视频讲解(四五课)

    内存的划分: 1,寄存器. 2,本地方法区. 3,方法区. 4,栈内存. 存储的都是局部变量. 而且变量所属的作用域一旦结束,该变量就自动释放. 5,堆内存. 存储是数组和对象(其实数组就是对象) 凡 ...

  3. eclipse安装反编译插件

    1. 进入http://jadclipse.sourceforge.net/wiki/index.php/Main_Page#Download          下载 net.sf.jadclipse ...

  4. XE2编译出来的DLL的DLLMain的退出地方用到了halt0

    DelphiXE2内存加模块升级版.支持32位和64位模块. 已转至新的博客 http://www.raysoftware.cn/?p=51 很多年以前写过内存加载DLL的一片技术. http://b ...

  5. Photoshop:建议设置

    一.新建文档设置: 二.对齐设置 菜单->视图->对齐->全部 使用图层.形状等操作时自动对齐网格,画矢量图不怕模糊边缘,确保每个像素保持清晰. 三.首选项设置 关掉"启用 ...

  6. 259. 3Sum Smaller

    题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...

  7. Google Hacking技术

    (1)google hack database https://www.exploit-db.com/google-hacking-database/ (2)查找包含某关键字的特定类型文件 命令:关键 ...

  8. Android Navigation Drawer,自定义ActionBar(标题居中)

    整个示例都是改造自 Google Android Training 中的 NavigationDrawer 示例(http://developer.android.com/training/imple ...

  9. .net 生成拼音码与五笔码

    首先加入配置文件: <?xml version="1.0" encoding="utf-8" ?> <CodeConfig> <S ...

  10. 面向对象设计Object Oriented Design

    http://www.codeproject.com/Articles/93369/How-I-explained-OOD-to-my-wife http://www.cnblogs.com/niyw ...