File tree Expand file tree Collapse file tree 1 file changed +65
-0
lines changed
Expand file tree Collapse file tree 1 file changed +65
-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 Main {
6+ static int N ;
7+ static int [][] arr;
8+ static int [][] visited;
9+
10+ public static void main (String [] args ) throws IOException {
11+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
12+ StringTokenizer st = new StringTokenizer (br. readLine());
13+ N = nextInt(st);
14+ int M = nextInt(st);
15+
16+ arr = new int [N ][N ];
17+ visited = new int [N ][N ];
18+ for (int i = 0 ; i < M ; i++ ) {
19+ st = new StringTokenizer (br. readLine());
20+ arr[nextInt(st) - 1 ][nextInt(st) - 1 ] = 1 ;
21+ }
22+
23+ for (int i = 0 ; i < N ; i++ ) {
24+ bfs(i);
25+ }
26+
27+ int answer = 0 ;
28+ for (int i = 0 ; i < N ; i++ ) {
29+ boolean good = true ;
30+ for (int j = 0 ; j < N ; j++ ) {
31+ if (i == j) continue ;
32+ if (visited[i][j] + visited[j][i] < 1 ) {
33+ good = false ;
34+ }
35+ }
36+ if (good) {
37+ answer++ ;
38+ }
39+ }
40+
41+ System . out. println(answer);
42+ }
43+
44+ private static void bfs (int start ) {
45+ Queue<Integer > queue = new LinkedList<> ();
46+ queue. add(start);
47+
48+ while (! queue. isEmpty()) {
49+ int target = queue. poll();
50+
51+ for (int i = 0 ; i < N ; i++ ) {
52+ if (arr[target][i] == 1 && visited[start][i] != 1 ) {
53+ visited[start][i] = 1 ;
54+ queue. add(i);
55+ }
56+ }
57+ }
58+
59+ }
60+
61+ private static int nextInt (StringTokenizer st ) {
62+ return Integer . parseInt(st. nextToken());
63+ }
64+ }
65+ ```
You can’t perform that action at this time.
0 commit comments