Skip to content

Commit f538bd1

Browse files
authored
[20250807] BOJ / G5 / 소셜 네트워킹 어플리케이션 / 김수연
1 parent 5874a2e commit f538bd1

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class boj7511 {
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+
static StringBuilder sb = new StringBuilder();
11+
12+
static int n, m;
13+
static int[] parents;
14+
public static void main(String[] args) throws Exception {
15+
nextLine();
16+
int t = nextInt();
17+
for(int a = 1 ; a <= t ; a++){
18+
nextLine();
19+
n = nextInt();
20+
21+
for(int i = 0 ; i < n ; i++) {
22+
parents[i] = i;
23+
}
24+
25+
nextLine();
26+
m = nextInt();
27+
28+
for(int i = 0 ; i < m ; i++) {
29+
nextLine();
30+
int v1 = nextInt();
31+
int v2 = nextInt();
32+
33+
if(find(v1) != find(v2)) {
34+
union(v1,v2);
35+
}
36+
}
37+
38+
nextLine();
39+
int k = nextInt();
40+
41+
sb.append("Scenario "+a+":\n");
42+
43+
for(int i = 0 ; i < k ; i++){
44+
nextLine();
45+
int v1 = nextInt();
46+
int v2 = nextInt();
47+
48+
if(find(v1) == find(v2)) {
49+
sb.append("1\n");
50+
}else{
51+
sb.append("0\n");
52+
}
53+
}sb.append("\n");
54+
}
55+
56+
System.out.println(sb);
57+
br.close();
58+
}
59+
60+
static int find(int x) {
61+
if(x == parents[x])
62+
return parents[x];
63+
64+
return parents[x] = find(parents[x]);
65+
}
66+
67+
static void union(int x,int y) {
68+
x = find(x);
69+
y = find(y);
70+
71+
if(x < y)
72+
parents[y] = x;
73+
else
74+
parents[x] = y;
75+
}
76+
}
77+
```

0 commit comments

Comments
 (0)