File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.util.* ;
3+ import java.io.* ;
4+
5+ public class boj14567 {
6+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
7+ static StringTokenizer st;
8+ static void nextLine () throws Exception {st = new StringTokenizer (br .readLine ());}
9+ static int nextInt() {return Integer . parseInt(st. nextToken());}
10+
11+ public static void main(String [] args) throws Exception {
12+ nextLine();
13+ int N = nextInt();
14+ int M = nextInt();
15+ List<List<Integer > > graph = new ArrayList<> ();
16+ Queue<Integer > q = new LinkedList<> ();
17+ int [] degree = new int [N + 1 ];
18+ int [] answer = new int [N + 1 ];
19+
20+ for (int i = 0 ; i <= N ; i++ ) graph. add(new ArrayList<> ());
21+ for (int i = 0 ; i < M ; i++ ) {
22+ nextLine();
23+ int a = nextInt();
24+ int b = nextInt();
25+ graph. get(a). add(b);
26+ degree[b]++ ;
27+ }
28+ for (int i = 1 ; i <= N ; i++ ) if (degree[i] == 0 ) q. offer(i);
29+ int cnt = 1 ;
30+ while (! q. isEmpty()) {
31+ int size = q. size();
32+ while (size-- > 0 ) {
33+ int cur = q. poll();
34+ answer[cur] = cnt;
35+ for (int next : graph. get(cur)) {
36+ if (-- degree[next] == 0 ) q. offer(next);
37+ }
38+ }
39+ cnt++ ;
40+ }
41+ for (int i = 1 ; i <= N ; i++ ) System . out. print(answer[i] + " " );
42+ }
43+ }
44+ ```
You can’t perform that action at this time.
0 commit comments