-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplanetsAndKingdoms.cpp
More file actions
71 lines (58 loc) · 1.37 KB
/
Copy pathplanetsAndKingdoms.cpp
File metadata and controls
71 lines (58 loc) · 1.37 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
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100001];
vector<int> rg[100001];
int visited[100001];
int stronglyConnectedComponent = 0;
vector<int> kingdom(100001, -1);
void dfs1(int x, stack<int> &s) {
visited[x] = 1;
for(int i = 0; i < g[x].size(); i++) {
int y = g[x][i];
if(visited[y] == 0)
dfs1(y, s);
}
s.push(x);
}
void dfs2(int x) {
// cout << x << " ";
visited[x] = 1;
kingdom[x] = stronglyConnectedComponent;
for(int i = 0; i < rg[x].size(); i++) {
int y = rg[x][i];
if(visited[y] == 0)
dfs2(y);
}
}
int main() {
int n, m, x, y;
cin >> n >> m;
for(int i = 1; i <= m; i++) {
cin >> x >> y;
g[x].push_back(y);
rg[y].push_back(x);
}
memset(visited, 0, sizeof(visited));
stack<int> s;
for(int i = 1; i <= n; i++) {
if(visited[i] == 0)
dfs1(i, s);
}
memset(visited, 0, sizeof(visited));
while(!s.empty()) {
int x = s.top();
s.pop();
if(visited[x] == 0) {
// cout << x << "----\n";
++stronglyConnectedComponent;
dfs2(x);
// cout << endl;
}
}
cout << stronglyConnectedComponent << endl;
for(int i = 1; i <= n; i++) {
cout << kingdom[i] << " ";
}
cout << endl;
return 0;
}