-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBOJ9470.java
More file actions
95 lines (75 loc) · 2.55 KB
/
BOJ9470.java
File metadata and controls
95 lines (75 loc) · 2.55 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class BOJ9470 {
static StringBuilder sb = new StringBuilder();
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static int m, p;
static int[] inDegree, strahler;
static boolean[] plusOne;
static List<Integer>[] graph;
private static void input() throws IOException {
st = new StringTokenizer(br.readLine());
sb.append(st.nextToken()).append(" "); // 테스트 케이스 번호
m = Integer.parseInt(st.nextToken());
p = Integer.parseInt(st.nextToken());
inDegree = new int[m + 1];
strahler = new int[m + 1];
plusOne = new boolean[m+1];
graph = new List[m + 1];
for (int i = 1; i <= m; i++) {
graph[i] = new ArrayList<>();
}
for (int i = 0; i < p; i++) {
st = new StringTokenizer(br.readLine());
int parent = Integer.parseInt(st.nextToken());
int child = Integer.parseInt(st.nextToken());
graph[parent].add(child);
inDegree[child]++;
}
}
public static void main(String[] args) throws IOException {
int testCase = Integer.parseInt(br.readLine());
for (int i = 0; i < testCase; i++) {
input();
process();
}
// print answer
System.out.println(sb.toString());
}
private static void process() {
topologicalSorting();
}
private static void topologicalSorting() {
Queue<Integer> queue = new LinkedList<>();
// set init
for (int i = 1; i <= m; i++) {
if(inDegree[i] == 0){
queue.add(i);
strahler[i] = 1;
plusOne[i] = true;
}
}
//
while(!queue.isEmpty()){
int thisVertex = queue.poll();
for(int i : graph[thisVertex]){
inDegree[i]--;
// update strahler
if(strahler[i] < strahler[thisVertex])
strahler[i] = strahler[thisVertex];
else if(!plusOne[i] && strahler[i] == strahler[thisVertex]){
strahler[i]++;
plusOne[i] = true;
}
if(inDegree[i] == 0){
queue.add(i);
}
}
}
// answer append
sb.append(strahler[m]).append("\n");
}
}