Skip to content

Commit 4091d09

Browse files
committedJun 9, 2024
BOJ_24542 : 튜터-튜티 관계의 수
1 parent 3789ffe commit 4091d09

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
public static int[] parent;
6+
public static int[] count;
7+
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
StringTokenizer st = new StringTokenizer(br.readLine());
11+
int N = Integer.parseInt(st.nextToken());
12+
int M = Integer.parseInt(st.nextToken());
13+
parent = new int[N+1];
14+
count = new int[N+1];
15+
for(int i=0; i<N; i++) {
16+
parent[i] = i;
17+
count[i] = 1;
18+
}
19+
for(int i=0; i<M; i++) {
20+
st = new StringTokenizer(br.readLine());
21+
int tutor = Integer.parseInt(st.nextToken());
22+
int tutee = Integer.parseInt(st.nextToken());
23+
union(tutor, tutee);
24+
}
25+
long answer = 1;
26+
for(int i=0; i<N; i++) {
27+
answer = (answer * count[i]) % 1000000007;
28+
}
29+
System.out.println(answer);
30+
}
31+
32+
public static int find(int x) {
33+
if(parent[x] == x) {
34+
return x;
35+
}
36+
parent[x] = find(parent[x]);
37+
return parent[x];
38+
}
39+
public static void union(int x, int y) {
40+
x = find(x);
41+
y = find(y);
42+
if(x == y) {
43+
return;
44+
}
45+
count[x] += count[y];
46+
count[y] = 1;
47+
parent[y] = x;
48+
}
49+
}

0 commit comments

Comments
 (0)
Please sign in to comment.