标签:bre sam 路径 body container sans show baseline 程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# include <cstdio> # include <cstring> # include <iostream> # include <cstring> # include <vector> # include <stack> # include <algorithm> using namespace std; #define N 10003 int dfn[N],low[N],ins[N],Time,num;//ins是否在栈里 vector< int >gra[N]; stack< int >sta; void Tarjan( int s) { dfn[s] = low[s] = ++Time; sta.push(s); ins[s] = 1 ; for ( int i= 0 ;i<gra[s].size();i++) { int k = gra[s][i]; if (dfn[k] == 0 ){ Tarjan(k); low[s] = min(low[s] ,low[k]); } if (dfn[k] != 0 && ins[k] == 1 ){ low[s] = min(low[s] ,dfn[k]);//low[s] = min(low[s] ,low[k]);好像也是对的 } } if (dfn[s] == low[s]) { num++; while (!sta.empty()) { int temp = sta.top(); sta.pop(); ins[temp] = 0 ; if (temp == s) break ; } } } int main() { int n,m; while (scanf( "%d%d" ,&n,&m)&&(n + m)) { memset(dfn, 0 ,sizeof(dfn)); memset(ins, 0 ,sizeof(ins)); memset(low, 0 ,sizeof(low)); while (!sta.empty()) sta.pop(); for ( int i= 1 ;i<=n;i++) gra[i].clear(); int a,b; while (m--) { scanf( "%d%d" ,&a,&b); gra[a].push_back(b); } num = Time = 0 ; for ( int i= 1 ;i<=n;i++) { if (dfn[i]== 0 ) Tarjan(i); } if (num > 1 ) printf( "No\n" ); else printf( "Yes\n" ); } } |
HDU1269迷宫城堡(裸Tarjan有向图求强连通分量个数)
标签:bre sam 路径 body container sans show baseline 程序
原文地址:http://www.cnblogs.com/liwenchi/p/7259327.html