diff --git "a/lkhyun/202506/30 BOJ G4 \354\202\254\354\235\264\355\201\264 \352\262\214\354\236\204.md" "b/lkhyun/202506/30 BOJ G4 \354\202\254\354\235\264\355\201\264 \352\262\214\354\236\204.md" new file mode 100644 index 00000000..56ce693b --- /dev/null +++ "b/lkhyun/202506/30 BOJ G4 \354\202\254\354\235\264\355\201\264 \352\262\214\354\236\204.md" @@ -0,0 +1,58 @@ +```java +import java.util.*; +import java.io.*; + +public class Main{ + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + static int n,m; + static int[] parent; + + public static void main(String[] args) throws Exception{ + st = new StringTokenizer(br.readLine()); + n = Integer.parseInt(st.nextToken()); + m = Integer.parseInt(st.nextToken()); + + parent = new int[n]; + for (int i = 0; i < n; i++) { + parent[i] = i; + } + + int ans = 0; + for (int i = 1; i <= m; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + if(union(a,b)){ + ans = i; + break; + } + } + bw.write(ans+""); + bw.close(); + } + static int find(int cur){ + if(cur == parent[cur]){ + return cur; + }else{ + return parent[cur] = find(parent[cur]); + } + } + static boolean union(int a, int b){ + int rootA = find(a); + int rootB = find(b); + + if(rootA == rootB){ + return true; + }else{ + if(rootA < rootB){ + parent[rootA] = rootB; + }else{ + parent[rootB] = rootA; + } + return false; + } + } +} +```