-
Notifications
You must be signed in to change notification settings - Fork 7
/
2186 Popular Cows.java
93 lines (92 loc) · 2.15 KB
/
2186 Popular Cows.java
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
import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
private static int ind = 0;
private static int index = 0;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int cowNumber = in.nextInt();
int pathNumber = in.nextInt();
List<List<Integer>> arc = new ArrayList<List<Integer>>();
boolean[] visited = new boolean[cowNumber + 1];
int[] dfn = new int[cowNumber + 1];
int[] low = new int[cowNumber + 1];
int[] count = new int[cowNumber + 1];
int[] id = new int[cowNumber + 1];
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i <= cowNumber; ++i) {
arc.add(new ArrayList<Integer>());
}
for (int i = 1; i <= pathNumber; ++i) {
int u = in.nextInt();
int v = in.nextInt();
arc.get(u).add(v);
}
for (int i = 0; i <= cowNumber; ++i) {
visited[i] = false;
dfn[i] = -1;
count[i] = 0;
}
for (int i = 1; i <= cowNumber; ++i) {
if (dfn[i] == -1) {
tarjan(i, visited, stack, dfn, low, count, id, arc);
}
}
for (int i = 0; i < ind; ++i) {
dfn[i] = 0;
}
for (int i = 1; i <= cowNumber; ++i) {
int size = arc.get(i).size();
int u = id[i];
for (int j = 0; j < size; ++j) {
int v = id[arc.get(i).get(j)];
if (u != v) {
dfn[u]++;
}
}
}
int flag = 0;
int rst = 0;
for (int i = 0; i < ind; ++i) {
if (dfn[i] == 0) {
rst = count[i];
flag++;
}
}
if (flag != 1) {
rst = 0;
}
System.out.println(rst);
}
private static void tarjan(int u, boolean[] visited, Stack<Integer> stack, int[] dfn, int[] low, int[] count, int[] id, List<List<Integer>> arc) {
visited[u] = true;
stack.push(u);
dfn[u] = index++;
low[u] = dfn[u];
int size = arc.get(u).size();
for (int i = 0; i < size; ++i) {
int v = arc.get(u).get(i);
if (dfn[v] == -1) {
tarjan(v, visited, stack, dfn, low, count, id, arc);
if (low[u] > low[v]) {
low[u] = low[v];
}
} else if (visited[v] && dfn[v] < low[u]) {
low[u] = dfn[v];
}
}
if (dfn[u] == low[u]) {
while (true) {
int v = stack.pop();
visited[v] = false;
count[ind]++;
id[v] = ind;
if (u == v) {
break;
}
}
ind++;
}
}
}