-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReact_Developer
More file actions
64 lines (62 loc) · 1.84 KB
/
React_Developer
File metadata and controls
64 lines (62 loc) · 1.84 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
package Daily_Problem;
import java.util.*;
public class React_Developer {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int mem_id[] = new int[num];
for (int i = 0; i < num; i++) {
mem_id[i] = sc.nextInt();
}
int n2 = sc.nextInt();
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
for (int i = 0; i < num; i++) {
adj.add(new ArrayList<>());
}
for (int i = 0; i < n2; i++) {
int num1 = sc.nextInt();
int num2 = sc.nextInt();
int index1 = -1, index2 = -1;
for (int j = 0; j < num; j++) {
if (mem_id[j] == num1) {
index1 = j;
}
if (mem_id[j] == num2) {
index2 = j;
}
}
if (index1 != -1 && index2 != -1) {
adj.get(index1).add(index2);
}
}
int per1 = sc.nextInt();
int per2 = sc.nextInt();
int indexPer1 = -1, indexPer2 = -1;
for (int i = 0; i < num; i++) {
if (mem_id[i] == per1) {
indexPer1 = i;
}
if (mem_id[i] == per2) {
indexPer2 = i;
}
}
boolean[] vis = new boolean[num];
if (indexPer1 != -1 && indexPer2 != -1) {
dfs(adj, vis, indexPer1);
}
if (vis[indexPer2]) {
System.out.println("1");
} else {
System.out.println("0");
}
sc.close();
}
public static void dfs(ArrayList<ArrayList<Integer>> adj, boolean[] vis, int per1) {
vis[per1] = true;
for (int it : adj.get(per1)) {
if (!vis[it]) {
dfs(adj, vis, it);
}
}
}
}