-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2252.java
More file actions
78 lines (69 loc) · 2.18 KB
/
Copy path2252.java
File metadata and controls
78 lines (69 loc) · 2.18 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
// https://www.acmicpc.net/problem/2252
// 줄 세우기
import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
private static int n, m;
private static Graph graph;
public static void main(String[] args) throws IOException {
// input
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(reader.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
graph = new Graph(n);
for(int i=0; i<m; i++){
st = new StringTokenizer(reader.readLine());
int a = Integer.parseInt(st.nextToken()) - 1;
int b = Integer.parseInt(st.nextToken()) - 1;
graph.add(a, b);
}
//long startTime = System.currentTimeMillis();
StringBuilder sb = new StringBuilder();
Queue<Integer> que = new LinkedList<>();
for(int node : graph.getNoEntryNodes())
que.add(node);
while(!que.isEmpty()){
int cur = que.poll();
sb.append(cur+1).append(' ');
graph.remove(cur);
for(int node : graph.getNoEntryNodes())
que.add(node);
}
System.out.println(sb);
//System.out.println(System.currentTimeMillis() - startTime + "ms");
}
}
class Graph{
private List<Integer>[] graph;
private int[] entry; // count entry
private boolean[] visited;
public Graph(int n){
graph = new List[n];
for(int i=0; i<n; i++)
graph[i] = new LinkedList<>();
entry = new int[n];
visited = new boolean[n];
}
public void add(int a, int b){
graph[a].add(b);
entry[b]++;
}
public List<Integer> getNoEntryNodes(){
List<Integer> result = new ArrayList<>();
for(int i=0; i<entry.length; i++){
if(visited[i]) continue;
if(entry[i] == 0){
result.add(i);
visited[i] = true;
}
}
return result;
}
public void remove(int rn){
for(int cn : graph[rn])
entry[cn]--;
//graph[rn].clear();
}
}