bzoj 1051 tarjan强连通分量
2013-11-20 14:32
330 查看
2013-11-16 11:39
原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1051
强连通分量,缩完点之后看出度为0的强连通分量有几个,如果只有一个则输出该强连通分量的点数,否则输出0;
原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1051
强连通分量,缩完点之后看出度为0的强连通分量有几个,如果只有一个则输出该强连通分量的点数,否则输出0;
/************************************************************** Problem: 1051 User: BLADEVIL Language: Pascal Result: Accepted Time:124 ms Memory:1396 kb ****************************************************************/ //By BLADEVIL var n, m :longint; pre, other :array[0..100010] of longint; last :array[0..20010] of longint; l :longint; flag :array[0..10010] of boolean; dfn, low :array[0..10010] of longint; time :longint; tot :longint; stack :array[0..10010] of longint; key :array[0..10010] of longint; size :array[0..10010] of longint; full :longint; father :array[0..20010] of 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; end; procedure init; var i :longint; x, y :longint; begin read(n,m); for i:=1 to m do begin read(x,y); connect(y,x); end; end; procedure dfs(x:longint); var q, p :longint; cur :longint; begin inc(time); dfn[x]:=time; low[x]:=time; flag[x]:=true; inc(tot); stack[tot]:=x; q:=last[x]; while q<>0 do begin p:=other[q]; if dfn[p]=0 then begin dfs(p); low[x]:=min(low[x],low[p]); end else if flag[p] then low[x]:=min(low[x],dfn[p]); q:=pre[q]; end; cur:=-1; if dfn[x]=low[x] then begin while cur<>x do begin cur:=stack[tot]; dec(tot); flag[cur]:=false; key[cur]:=x+n; inc(size[key[cur]]); end; end; end; procedure main; var i :longint; q, p :longint; begin for i:=1 to n do if dfn[i]=0 then dfs(i); for i:=1 to n do begin q:=last[i]; while q<>0 do begin p:=other[q]; if key[i]<>key[p] then begin connect(key[i],key[p]); father[key[p]]:=key[i]; end; q:=pre[q]; end; end; full:=-1; for i:=1 to n do begin if (father[key[i]]=0) and (full=-1) then full:=key[i]; if (father[key[i]]=0) and (full<>-1) and (full<>key[i]) then begin writeln(0); exit; end; end; writeln(size[full]); end; begin init; main; end.
相关文章推荐
- [BZOJ1051] [HAOI2006] 受欢迎的牛 - tarjan强连通分量
- 【强联通分量缩点】【Tarjan】bzoj1051 [HAOI2006]受欢迎的牛
- 【连通分量】BZOJ 1051:[HAOI2006]受欢迎的牛
- [BZOJ2330][SCOI2011][拓扑排序][强连通分量][Tarjan]Candy
- [BZOJ1051][HAOI2006] 受欢迎的牛 tarjan求联通分量
- BZOJ 1179: [Apio2009]Atm Tarjan强连通分量缩点,SPFA,DP
- [BZOJ 2140] 稳定婚姻 Tarjan求强连通分量
- 【BZOJ 1093】【ZJOI 2007】【最大半连通子图】【tarjan强连通分量】【拓扑排序dp】
- tarjan算法之 强连通分量
- POJ1236强连通分量tarjan
- hdu1269--求强连通分量个数--tarjan--图的静态邻接表
- 图论练习-有向图的强连通分量【tarjan】
- bzoj 1051[HAOI2006]受欢迎的牛|tarjan|水题
- 强连通分量 Tarjan 算法入门笔记
- [poj 1904]King's Quest[Tarjan强连通分量]
- [BZOJ1051]HAOI2006受欢迎的牛|强联通分量
- [BZOJ 1051][POJ 2186] 受欢迎的牛/Popular Cows Tarjan缩点+判断出度
- bzoj1051 [HAOI2006]受欢迎的牛(tarjan求强连通分量)
- HDU 1269 迷宫城堡 (有向图强连通分量Tarjan)
- bzoj1051 [HAOI2006]受欢迎的牛[图论][tarjan缩点]