-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaekjoon_1976.java
More file actions
90 lines (64 loc) · 2.19 KB
/
baekjoon_1976.java
File metadata and controls
90 lines (64 loc) · 2.19 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
79
80
81
82
83
84
85
86
87
88
89
90
import java.io.*;
import java.util.*;
public class baekjoon_1976 {
static int N, M;
static boolean[] used;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int M = Integer.parseInt(br.readLine());
List<List<Integer>> graph = new ArrayList<>();
for(int i=0;i<=N;i++) {
graph.add(new ArrayList<>());
}
for(int i=1;i<=N;i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for(int j=1;j<=N;j++) {
int conn = Integer.parseInt(st.nextToken());
if(conn == 1) {
graph.get(j).add(i);
graph.get(i).add(j);
}
}
}
StringTokenizer st = new StringTokenizer(br.readLine());
int[] dest = new int[M];
for(int i=0;i<M;i++) {
dest[i] = Integer.parseInt(st.nextToken());
}
boolean result = false;
for(int i=1;i<M;i++) {
Queue<Integer> queue = new LinkedList<>();
int start = dest[i-1];
int next = dest[i];
boolean find = false;
boolean[] visited = new boolean[N + 1];
if(start == next) {
find = true;
}
else {
queue.add(start);
}
while(!queue.isEmpty()) {
int cur = queue.poll();
for(int neighbor : graph.get(cur)) {
if(visited[neighbor]) {
continue;
}
if(neighbor == next) {
find = true;
break;
}
visited[neighbor] = true;
queue.add(neighbor);
}
}
if(!find) {
result = false;
break;
}
else { result = true; }
}
System.out.println((result) ? "YES" : "NO");
}
}