Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions suyeun84/202508/07 BOJ 소셜 네트워킹 어플리케이션.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
```java
import java.io.*;
import java.util.*;

public class boj7511 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static int nextInt() {return Integer.parseInt(st.nextToken());}
static StringBuilder sb = new StringBuilder();

static int n, m;
static int[] parents;
public static void main(String[] args) throws Exception {
nextLine();
int t = nextInt();
for(int a = 1 ; a <= t ; a++){
nextLine();
n = nextInt();

for(int i = 0 ; i < n ; i++) {
parents[i] = i;
}

nextLine();
m = nextInt();

for(int i = 0 ; i < m ; i++) {
nextLine();
int v1 = nextInt();
int v2 = nextInt();

if(find(v1) != find(v2)) {
union(v1,v2);
}
}

nextLine();
int k = nextInt();

sb.append("Scenario "+a+":\n");

for(int i = 0 ; i < k ; i++){
nextLine();
int v1 = nextInt();
int v2 = nextInt();

if(find(v1) == find(v2)) {
sb.append("1\n");
}else{
sb.append("0\n");
}
}sb.append("\n");
}

System.out.println(sb);
br.close();
}

static int find(int x) {
if(x == parents[x])
return parents[x];

return parents[x] = find(parents[x]);
}

static void union(int x,int y) {
x = find(x);
y = find(y);

if(x < y)
parents[y] = x;
else
parents[x] = y;
}
}
```