diff --git "a/suyeun84/202506/18 BOJ G3 \353\202\230\353\247\214 \354\225\210\353\220\230\353\212\224 \354\227\260\354\225\240.md" "b/suyeun84/202506/18 BOJ G3 \353\202\230\353\247\214 \354\225\210\353\220\230\353\212\224 \354\227\260\354\225\240.md" new file mode 100644 index 00000000..cace99be --- /dev/null +++ "b/suyeun84/202506/18 BOJ G3 \353\202\230\353\247\214 \354\225\210\353\220\230\353\212\224 \354\227\260\354\225\240.md" @@ -0,0 +1,70 @@ +```java +import java.io.*; +import java.util.*; + +public class boj14621 { + 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 int N, M, answer; + static char[] gender; + static Road[] roads; + static int[] parent; + public static void main(String[] args) throws Exception { + nextLine(); + N = nextInt(); + M = nextInt(); + answer = 0; + nextLine(); + gender = new char[N+1]; + roads = new Road[M]; + parent = new int[N+1]; + for (int i = 1; i <= N; i++) { + gender[i] = st.nextToken().charAt(0); + parent[i] = i; + } + for (int i = 0; i < M; i++) { + nextLine(); + int u = nextInt(); + int v = nextInt(); + int d = nextInt(); + roads[i] = new Road(u, v, d); + } + Arrays.sort(roads, (o1, o2) -> o1.d-o2.d); + int cnt = 0; + for (int i = 0; i < M; i++) { + if (gender[roads[i].s] == gender[roads[i].e]) continue; + if (find(roads[i].s) == find(roads[i].e)) continue; + answer += roads[i].d; + union(roads[i].s, roads[i].e); + cnt++; + } + if (cnt != N-1) System.out.println(-1); + else System.out.println(answer); + } + + static int find(int x) { + if (x == parent[x]) return x; + return parent[x] = find(parent[x]); + } + + static void union(int x, int y) { + x = parent[x]; + y = parent[y]; + + if (x