2013-09-08 10:00

var
m, n :longint;
t :longint;
f, last :array[..] of longint;
pre, other :array[..] of longint;
l, time :longint;
dfn, low :array[..] of longint;
tot :longint;
stack :array[..] of longint;
flag, fs :array[..] of boolean;
i :longint;
key :array[..] of longint;
kk :longint;
que :array[..] of longint;
count :longint; function min(a,b:longint):longint;
begin
if a>b then min:=b else min:=a;
end; procedure connect(x,y:longint);
begin
inc(l);
pre[l]:=last[x];
last[x]:=l;
other[l]:=y;
f[y]:=x;
end; procedure init;
var
i :longint;
x, y :longint;
begin
read(n,m);
for i:= to m do
begin
read(x,y);
connect(x,y);
end;
end; procedure dfs(x:longint);
var
p, q, cur :longint;
begin
inc(time);
dfn[x]:=time;
low[x]:=time;
inc(tot);
stack[tot]:=x;
fs[x]:=true;
flag[x]:=true;
q:=last[x];
while q<> do
begin
p:=other[q];
if p<>x then
begin
if not flag[p] then
begin
dfs(p);
low[x]:=min(low[x],low[p]);
end else
if fs[p] then
begin
low[x]:=min(low[x],dfn[p]);
end;
end;
q:=pre[q];
end;
p:=-;
if low[x]=dfn[x] then
begin
inc(kk);
while p<>x do
begin
p:=stack[tot];
fs[p]:=false;
key[p]:=kk;
dec(tot);
inc(count);
end;
end; end; function bfs(x:longint):boolean;
var
i :longint;
t, h, p, q :longint;
cur :longint;
d :array[..] of longint; begin
fillchar(flag,sizeof(flag),);
fillchar(d,sizeof(d),);
h:=; t:=;
que[]:=x;
d[x]:=;
while h<t do
begin
inc(h);
cur:=que[h];
q:=last[cur];
while q<> do
begin
p:=other[q];
inc(t);
que[t]:=p;
d[p]:=d[cur]+;
q:=pre[q];
end;
end;
if d[que[t]]=kk-n then exit(true) else exit(false);
end; procedure main;
var
i :longint;
x :longint;
q, p :longint;
begin
l:=;
fillchar(last,sizeof(last),);
time:=;
fillchar(f,sizeof(f),);
fillchar(low,sizeof(low),);
fillchar(dfn,sizeof(dfn),);
fillchar(flag,sizeof(flag),false);
fillchar(stack,sizeof(stack),);
tot:=;
fillchar(fs,sizeof(fs),false);
fillchar(key,sizeof(key),);
count:=;
init;
x:=;
kk:=n;
for i:= to n do
if (f[i]=) then
begin
if x<> then
begin
writeln('No');
exit;
end;
x:=i;
end;
if x= then x:=;
dfs(x); if count<>n then
begin
writeln('No');
exit;
end; for i:= to n do
begin
q:=last[i];
while q<> do
begin
p:=other[q];
if key[i]<>key[p] then connect(key[i],key[p]);
q:=pre[q];
end;
end;
x:=;
for i:=n+ to kk do
begin
if f[i]= then
begin
if x<> then
begin
writeln('No');
exit;
end;
x:=i;
end;
end; if x= then x:=;
if bfs(x) then writeln('Yes') else writeln('No');
end; begin
read(t);
for i:= to t do main;
end.

poj 2762 tarjan缩点+拓扑序的更多相关文章

  1. POJ 2762 tarjan缩点+并查集+度数

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15494 ...

  2. poj 2762 强连通缩点+拓扑排序

    这题搞了好久,先是拓扑排序这里没想到,一开始自己傻乎乎的跑去找每层出度为1的点,然后才想到能用拓扑排序来弄. 拓扑排序的时候也弄了挺久的,拓扑排序用的也不多. 题意:给一个图求是否从对于任意两个点能从 ...

  3. POJ 2672 Tarjan + 缩点 + 拓扑思想

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17383 ...

  4. poj 2762(tarjan缩点+判断是否是单链)

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19234 ...

  5. UVA 11324.The Largest Clique tarjan缩点+拓扑dp

    题目链接:https://vjudge.net/problem/UVA-11324 题意:求一个有向图中结点数最大的结点集,使得该结点集中任意两个结点u和v满足:要目u可以到达v,要么v可以到达u(相 ...

  6. [HAOI2006]受欢迎的牛 tarjan缩点 + 拓扑排序

    ---题面--- 题解: 首先tarjan缩点应该还是容易想到的,因为喜爱具有传递性,所以一个强联通分量里面的点实际上是全部等效的,所以我们可以缩成一个方便判断, 缩完点之后整张图就变成了一个有向无环 ...

  7. HDU 6165 FFF at Valentine(Tarjan缩点+拓扑排序)

    FFF at Valentine Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. [ZJOI2007]最大半连通子图 (Tarjan缩点,拓扑排序,DP)

    题目链接 Solution 大概是个裸题. 可以考虑到,如果原图是一个有向无环图,那么其最大半联通子图就是最长的一条路. 于是直接 \(Tarjan\) 缩完点之后跑拓扑序 DP就好了. 同时由于是拓 ...

  9. [模板]tarjan缩点+拓扑排序

    题目:给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只计算一次. 题目简述:先t ...

随机推荐

  1. <Effective C++>读书摘要--Introduction

    Introduction 1.Learning the fundamentals of a programming language is one thing; learning how to des ...

  2. 用glob()函数返回目录下的子文件以及子目录

    glob() 函数返回匹配指定模式的文件名或目录 相对于readdir()和opendir()来说,使用glob()函数会方便很多 代码1: <?php function getfilename ...

  3. 【bzoj4921】[Lydsy六月月赛]互质序列 暴力

    题目描述 给出一个序列,要求删除一段非空区间,使得剩下的数的个数大于等于2.求所有删除方式剩下的数的最大公约数的和. 输入 第一行包含一个正整数n(3<=n<=100000),表示序列的长 ...

  4. hihoCoder#1698 : 假期计划 组合数

    题面:hihoCoder#1698 : 假期计划  组合数 题解: 题目要求是有序的排列,因此我们可以在一开始就乘上A!*B!然后在把这个序列划分成很多段. 这样的话由于乘了阶乘,所以所有排列我们都已 ...

  5. ZOJ1994 & POJ2396:Budget——题解

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1994 http://poj.org/problem?id=2396 题目大 ...

  6. Vue语法笔记

    Vue.js 的核心是一个允许采用简洁的模板语法来声明式的将数据渲染进 DOM: 事件监听:v-on 指令绑定一个事件监听器  缩写[@]   v-on:click 用户输入,绑定数据:v-model ...

  7. lightoj 1341

    lightoj 1341  Aladdin and the Flying Carpet 链接:http://lightoj.com/volume_showproblem.php?problem=134 ...

  8. mac之os x系统下搭建nodejs+express4.x+mongodb+gruntjs整套前端工程

    第一次在Mac OS X上搭建前端开发环境,做一个小小记录,包括一些与windows系统的区别和常用快捷键 首先,在进行环境搭建之前先来看一下苹果系统的“cmd”,也就是Terminal(终端). 打 ...

  9. margin和padding

    一.margin基础语法与结构 1.margin语法 Margin:10px Margin的值是数字+html单位,同时也可以为auto(自动.自适应) 2.应用结构 Div{margin:10px} ...

  10. 本地更新代码同步至github仓库

    昨晚在家里写了一个demo放到github上,然后今天晚上来公司准备搞一下,但是git pull下来在本地修改之后push不到github上,然后发现公司电脑上并没有access权限,然后想起来还没配 ...