Skip to content

Commit 85564c5

Browse files
authored
[20250630] BOJ / G4 / 사이클 게임 / 이강현
1 parent 3a476f4 commit 85564c5

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class Main{
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
static StringTokenizer st;
9+
static int n,m;
10+
static int[] parent;
11+
12+
public static void main(String[] args) throws Exception{
13+
st = new StringTokenizer(br.readLine());
14+
n = Integer.parseInt(st.nextToken());
15+
m = Integer.parseInt(st.nextToken());
16+
17+
parent = new int[n];
18+
for (int i = 0; i < n; i++) {
19+
parent[i] = i;
20+
}
21+
22+
int ans = 0;
23+
for (int i = 1; i <= m; i++) {
24+
st = new StringTokenizer(br.readLine());
25+
int a = Integer.parseInt(st.nextToken());
26+
int b = Integer.parseInt(st.nextToken());
27+
if(union(a,b)){
28+
ans = i;
29+
break;
30+
}
31+
}
32+
bw.write(ans+"");
33+
bw.close();
34+
}
35+
static int find(int cur){
36+
if(cur == parent[cur]){
37+
return cur;
38+
}else{
39+
return parent[cur] = find(parent[cur]);
40+
}
41+
}
42+
static boolean union(int a, int b){
43+
int rootA = find(a);
44+
int rootB = find(b);
45+
46+
if(rootA == rootB){
47+
return true;
48+
}else{
49+
if(rootA < rootB){
50+
parent[rootA] = rootB;
51+
}else{
52+
parent[rootB] = rootA;
53+
}
54+
return false;
55+
}
56+
}
57+
}
58+
```

0 commit comments

Comments
 (0)