标签:line 技术分享 中间 turn 判断 bsp back dfs include
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
|
# 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); } //num就是强连通分量的个数 } } |
for
(
int
i=
1
;i<=n;i++)
{
for
(
int
j=
0
;j<Gra[i].size();j++){
int
k = Gra[i][j];
if
(belong[i] != belong[k]){
outDegree[belong[i]]++;
}
}
}
其实也算不上重建图....只是求了一下各个节点的出度而已......
3.POJ2762
Tarjan + 拓扑排序
这个题是真的烦人,都给我WA哭了有木有,从中午2点错到晚上10点,中间气的我玩了几把游戏,真的气。
还有一个就是有人说缩晚点以后的DAG是一条链,也有好多人是这么写的,都AC了,说是dfs点的数目等于强连通块的数目就可以。
但是感觉明显不对啊,整个就是一个树,你dfs的点数肯定等于
强连通块的数目啊,不懂他们怎么A的。 感觉和2186差不多,但是最多只有1000个顶点。
我怎么这么愚蠢,居然想到要暴力!!!我太蠢啦!!!
学长一语点醒
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# include <cstdio> # include <cstring> # include <iostream> # include <string> # include <vector> # include <stack> # include < set > # include <algorithm> using namespace std; #define N 1002 vector< int >Gra[N]; stack< int >Sta; int map[N][N]; int dfn[N],low[N],inStack[N],belong[N],Time,cnt; int inDegree[N]; void init() { Time = cnt = 0 ; memset(dfn, 0 ,sizeof(dfn)); memset(low, 0 ,sizeof(dfn)); memset(inStack, 0 ,sizeof(inStack)); memset(inDegree, 0 ,sizeof(inDegree)); memset(belong, 0 ,sizeof(belong)); for ( int i= 0 ;i<N;i++) Gra[i].clear(); memset(map, 0 ,sizeof(map)); while (!Sta.empty()) Sta.pop(); } void Tarjan( int s) { dfn[s] = low[s] = ++Time; inStack[s] = 1 ; Sta.push(s); for ( int i= 0 ;i<Gra[s].size();i++) { int j = Gra[s][i]; if (dfn[j] == 0 ){ Tarjan(j); low[s] = min(low[s], low[j]); } else if (inStack[j] == 1 ){ low[s] = min(low[s], dfn[j]); } } if (dfn[s] == low[s]) { cnt ++; while (!Sta.empty()){ int temp = Sta.top(); Sta.pop(); inStack[temp] = 0 ; belong[temp] = cnt; if (temp == s) break ; } } return ; } void tsort() { for ( int k= 0 ;k<cnt;k++){ int fuck = 0 ,pos; for ( int i= 1 ;i<=cnt;i++) { if (inDegree[i] == 0 ) { fuck ++; pos = i; } } if (fuck > 1 ){ printf( "No\n" ); return ; } inDegree[pos ] = - 1 ; for ( int i= 1 ;i<=cnt;i++) { if (map[pos][i] == 1 ) inDegree[i]--; } } printf( "Yes\n" ); } int main() { int noc; cin>>noc; while (noc--) { init(); int n,m,x,y; scanf( "%d%d" ,&n,&m); for ( int i= 0 ;i<m;i++) { scanf( "%d%d" ,&x,&y); Gra[x].push_back(y); } for ( int i= 1 ;i<=n;i++) if (dfn[i] == 0 ) Tarjan(i); if (cnt == 1 ) { printf( "Yes\n" ); continue ; } for ( int i= 1 ;i<=n;i++) { for ( int j= 0 ;j<Gra[i].size();j++) { int k = Gra[i][j]; if (belong[i]!=belong[k]){ if (map[belong[i]][belong[k]] == 0 ){ map[belong[i]][belong[k]] = 1 ; inDegree[belong[k]]++; } } } } for ( int i= 1 ;i<=cnt;i++) printf( "%d %d\n" ,i,inDegree[i]); tsort(); } } |
for
(
int
i=
1
;i<=n;i++)
{
for
(
int
j=
0
;j<Gra[i].size();j++){
int
k = Gra[i][j];
if
(belong[i] != belong[k]){
outDegree[belong[i]]++;//inDegree[belong[i]]++;
}
}
}
就可以了。
但是假如要对入度出度进行操作就不可以了,因为强连通块1是由1,2,3节点构成,强连通块2是由4,5构成
现在缩点后块1 ----> 块2。其中节点2 ---> 4,
2 ---> 5,3 ---> 5。这样下来outdegree[1] == 3;标签:line 技术分享 中间 turn 判断 bsp back dfs include
原文地址:http://www.cnblogs.com/liwenchi/p/7259276.html