-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrong.cpp
More file actions
143 lines (91 loc) · 2.16 KB
/
strong.cpp
File metadata and controls
143 lines (91 loc) · 2.16 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <cstdio>
#include <vector>
#include <stack>
#include <cstring>
#define FIN "ctc.in"
#define FOUT "ctc.out"
#define MAX 100005
using namespace std;
int num_nodes,
num_arcs;
int num_comp = 1;
vector<int> List1[ MAX ];
vector<int> List2[ MAX ];
int SUCC[MAX];
int PREC[MAX];
//function prototypes
void read();
void DFS1(int node);
void DFS2(int node);
void solve();
void displayList();
void write();
int main() {
read();
solve();
write();
return(0);
};
void read() {
int x,y;
freopen(FIN, "r", stdin);
scanf("%d %d", &num_nodes, &num_arcs);
for(; num_arcs;num_arcs--) {
scanf("%d %d", &x, &y);
List1[ x ].push_back( y );
List2[ y ].push_back( x );
}
fclose( stdin );
};
void DFS1(int node) {
SUCC[ node ] = num_comp;
for(int i = 0; i < List1[ node ].size(); i++) {
if( !SUCC[ List1[ node ][ i ] ]) {
DFS1( List1[ node ][ i ]);
}
}
};
void DFS2(int node) {
PREC[ node ] = num_comp;
for(int j = 0; j < List2[ node ].size(); j++) {
if( !PREC[ List2[ node ][ j ] ]) {
DFS2( List2[ node ][ j ] );
}
}
};
void write() {
freopen(FOUT, "w", stdout);
printf("%d\n", num_comp - 1);
for(int k = 1; k <= num_comp - 1; k++) {
for(int p = 1; p <= num_nodes; p++) {
if(SUCC[p] == k) {
printf("%d ", p);
}
}
printf("\n");
}
fclose( stdin );
};
void solve() {
memset(SUCC, 0, sizeof(SUCC));
memset(PREC, 0, sizeof(PREC));
for(int i = 1; i <= num_nodes; i++) {
if(!SUCC[i]) {
DFS1(i);
DFS2(i);
for(int j = 1; j <= num_nodes; j++) {
if(SUCC[j] != PREC[j]) SUCC[j] = PREC[j] = 0;
}
num_comp++;
}
}
}
void displayList() {
for(int i = 1; i<=num_nodes; i++) {
printf("%d -> ", i);
for(int j = 0; j < List2[i].size(); j++) {
printf("%d ", List2[i][j]);
}
printf("\n");
}
}