Gifts by the List CodeForces - 681D (思维)
大意: 给定森林, 要求构造一个表, 满足对于每个$x$, 表中第一次出现的$x$的祖先(包括$x$)是$a_x$.
刚开始还想着直接暴力分块优化一下连边, 最后按拓扑序输出...
实际上可以发现$a_x$要么等于$x$, 要么等于$fa_x$, 直接dfs一边就好了...
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif int n, m;
vector<int> g[N];
int s[N], vis[N], a[N];
void dfs(int x) {
if (vis[x]) return;
vis[x] = 1;
for (int y:g[x]) {
if (a[y]!=y&&a[y]!=a[x]) puts("-1"),exit(0);
dfs(y);
}
if (a[x]==x) s[++*s]=x;
} int main() {
scanf("%d%d", &n, &m);
REP(i,1,m) {
int u, v;
scanf("%d%d", &u, &v);
g[u].pb(v);
}
REP(i,1,n) scanf("%d", a+i);
REP(i,1,n) dfs(i);
printf("%d\n", *s);
REP(i,1,*s) printf("%d\n", s[i]);
}
Gifts by the List CodeForces - 681D (思维)的更多相关文章
- 【33.33%】【codeforces 681D】Gifts by the List
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- codeforces 681D Gifts by the List dfs+构造
题意:给你一个森林,表示其祖先关系(自己也是自己的祖先),每个人有一个礼物(要送给这个人的固定的一个祖先) 让你构造一个序列,使得的对于每个人,这个序列中第一个出现的他的祖先,是他要送礼物的的那个祖先 ...
- CodeForces 681D Gifts by the List
$dfs$,后续遍历. 如果某个节点$a[i]=i$,那么$i$的后继的$a[i]$都要指向$i$,直到出现新的后继$j$,$a[j]=j$.利用这个可以判断是否有解. 如果有解的话,那么只要输出后序 ...
- CodeForces 681D Gifts by the List (树上DFS)
题意:一个家庭聚会,每个人都想送出礼物,送礼规则是, 一个人,先看名单列表,发现第一个祖先 就会送给他礼物,然后就不送了,如果他没找到礼物 他会伤心的离开聚会!告诉你m个祖先关系, 和每个人想给谁送! ...
- Codeforces 424A (思维题)
Squats Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Statu ...
- Codeforces 1060E(思维+贡献法)
https://codeforces.com/contest/1060/problem/E 题意 给一颗树,在原始的图中假如两个点连向同一个点,这两个点之间就可以连一条边,定义两点之间的长度为两点之间 ...
- Queue CodeForces - 353D (思维dp)
https://codeforces.com/problemset/problem/353/D 大意:给定字符串, 每一秒, 若F在M的右侧, 则交换M与F, 求多少秒后F全在M左侧 $dp[i]$为 ...
- codeforces 1244C (思维 or 扩展欧几里得)
(点击此处查看原题) 题意分析 已知 n , p , w, d ,求x , y, z的值 ,他们的关系为: x + y + z = n x * w + y * d = p 思维法 当 y < w ...
- CodeForces - 417B (思维题)
Crash Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Status ...
随机推荐
- Java 多线程案例
同步代码块 SynchronizedTest类,用来表示取票功能 package concurency.chapter6; public class SynchronizedTest implemen ...
- CIFAR-10与ImageNet图像识别
2.1.2 下载CIFAR-10 数据 python cifar10_download.py # Copyright 2015 The TensorFlow Authors. All Rights R ...
- 案例:8,64,256都是2的阶次方数(8是2的3次方),用Java编写程序来判断一个整数是不是2的阶次方数。
如果一个数是2的阶次方数,则它的二进制数的首位一般是1,后面全为0.比如8:1000,64:1000000,如果将这个数减1后再作与&运算,则应该全为0,(x&(x-1)==0&am ...
- jquery选择器扩展之样式选择器
https://github.com/wendux/style-selector-jQuery-plugin http://blog.csdn.net/duwen90/article/details/ ...
- C#解析html文档类库HtmlAgilityPack下载地址
新:http://html-agility-pack.net/?z=codeplex 原:http://htmlagilitypack.codeplex.com/
- hibernate的三种状态和缓存
hibernate的三种状态: 1.瞬时态:对象里面没有id值,对象与session没有关联 类似,把class类new出来,不存进session 2.持久态:对象里面有id值,对象与session关 ...
- 使用lombok 找不到方法
在setting里面查找并设置就好了
- Ajax同步
转载自:https://blog.csdn.net/xiegongmiao/article/details/78217386 AJAX中根据async的值不同分为同步(async = false)和异 ...
- 原生ajax的请求封装get和post
一个完整的AJAX请求包括五个步骤: 1.创建XMLHTTPRequest对象 2.使用open方法创建http请求,并设置请求地址 3.设置发送的数据,开始和服务器端交互 4.注册事件 5.获取响应 ...
- 大数据-Hive 常用命令
Hive 启动 ~$ hive 退出 hive>quit; --退出hive or hive> exit; --exit会影响之前的使用,所以需要下一句kill掉hadoop的进程 > ...