-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
76 lines (63 loc) Β· 2.27 KB
/
Main.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
package Graph.P21276;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int N, M;
static String[] names;
static HashMap<String, Integer> idx = new HashMap<>();
static LinkedList<Integer>[] children, graph;
static int[] indegree;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Graph/P21276/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
names = new String[N];
children = new LinkedList[N];
graph = new LinkedList[N];
indegree = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) names[i] = st.nextToken();
Arrays.sort(names);
for (int i = 0; i < N; i++) {
idx.put(names[i], i);
children[i] = new LinkedList<>();
graph[i] = new LinkedList<>();
}
M = Integer.parseInt(br.readLine());
while (M-- > 0) {
st = new StringTokenizer(br.readLine());
int X = idx.get(st.nextToken()), Y = idx.get(st.nextToken());
graph[Y].add(X);
indegree[X] ++;
}
Queue<Integer> q = new LinkedList<>();
LinkedList<Integer> root = new LinkedList<>();
for (int i = 0; i < N; i++) {
if (indegree[i] == 0) {
q.offer(i);
root.add(i);
}
}
while (!q.isEmpty()) {
int cur = q.poll();
for (int next : graph[cur]) {
indegree[next] --;
if (indegree[next] == 0) {
children[cur].add(next);
q.offer(next);
}
}
}
System.out.println(root.size());
for (Integer s : root) System.out.print(names[s] + " ");
System.out.println();
for (int i = 0; i < N; i++) {
System.out.print(names[i] + " " + children[i].size() + " ");
Collections.sort(children[i]);
for (Integer c : children[i]) System.out.print(names[c] + " ");
System.out.println();
}
}
}