-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlanetsandKingdoms.java
More file actions
78 lines (69 loc) · 2.17 KB
/
PlanetsandKingdoms.java
File metadata and controls
78 lines (69 loc) · 2.17 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;
public class PlanetsandKingdoms {
static int n, m;
static ArrayList<ArrayList<Integer>> al = new ArrayList<>();
static boolean[] vis;
static ArrayList<ArrayList<Integer>> transpose = new ArrayList<>();
static Stack<Integer> stack = new Stack<>();
public static void dfs(int i) {
vis[i] = true;
for(int j: al.get(i)) {
if(!vis[j]) {
dfs(j);
}
}
stack.push(i);
}
public static void dfsT(int i) {
vis[i] = true;
for(int j: transpose.get(i)) {
if(!vis[j]) {
dfsT(j);
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] s = br.readLine().split(" ");
n = Integer.parseInt(s[0]);
m = Integer.parseInt(s[1]);
vis = new boolean[n];
for(int i = 0; i < n; i++) {
al.add(new ArrayList<>());
transpose.add(new ArrayList<>());
}
for(int i = 0; i < m; i++) {
String[] str = br.readLine().split(" ");
al.get(Integer.parseInt(str[0]) - 1).add(Integer.parseInt(str[1]) - 1);
transpose.get(Integer.parseInt(str[1]) - 1).add(Integer.parseInt(str[0]) - 1);
}
Arrays.fill(vis, false);
for(int i = 0; i < n; i++) {
if (!vis[i]) {
dfs(i);
}
}
int[] ans = new int[n];
int previous = 0;
Arrays.fill(vis, false);
while(!stack.isEmpty()) {
int i = stack.pop();
if(!vis[i]) {
dfsT(i);
previous++;
ans[i] = previous;
} else {
ans[i] = previous;
}
}
System.out.println(previous);
for(int i : ans) {
System.out.print(i +" ");
}
}
}