From cd85e081655aeb8947dfb6e7c83e199cbc220e55 Mon Sep 17 00:00:00 2001 From: songyc macbook Date: Tue, 13 Jan 2026 22:44:15 +0900 Subject: [PATCH] baekjoon 20260113 --- code_study/Baekjoon/java/_1647_1.java | 68 +++++++++++++++++++++++++ code_study/Baekjoon/java/_1647_2.java | 71 +++++++++++++++++++++++++++ code_study/Baekjoon/python/25372.py | 2 + 3 files changed, 141 insertions(+) create mode 100644 code_study/Baekjoon/java/_1647_1.java create mode 100644 code_study/Baekjoon/java/_1647_2.java create mode 100644 code_study/Baekjoon/python/25372.py diff --git a/code_study/Baekjoon/java/_1647_1.java b/code_study/Baekjoon/java/_1647_1.java new file mode 100644 index 0000000..98634b3 --- /dev/null +++ b/code_study/Baekjoon/java/_1647_1.java @@ -0,0 +1,68 @@ +import java.util.*; + +public class _1647_1 { + static int[] parents; + static int[] rank; + + static int find(int x) { + if(x != parents[x]) return parents[x] = find(parents[x]); + return x; + } + + static void union(int x, int y) { + x = find(x); + y = find(y); + + if(x==y) return; + + if(rank[x] < rank[y]) { + int temp = x; + x = y; + y = temp; + } + + if(rank[x] == rank[y]) rank[x]++; + + parents[y] = x; + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int N = sc.nextInt(), M = sc.nextInt(); + + if(N==2) { + System.out.println(0); + return; + } + + ArrayList edges = new ArrayList<>(); + + for(int i=0; i a[2] - b[2]); + + parents = new int[N+1]; + for(int i=1; i<=N; i++) parents[i] = i; + rank = new int[N+1]; + + int cnt = 0, ans = 0; + + for (int[] edge : edges) { + int a = edge[0], b = edge[1], c = edge[2]; + + if(find(a) == find(b)) continue; + + union(a, b); + cnt += 1; + ans += c; + if(cnt == N-2) break; + } + + System.out.println(ans); + } +} diff --git a/code_study/Baekjoon/java/_1647_2.java b/code_study/Baekjoon/java/_1647_2.java new file mode 100644 index 0000000..67ffad8 --- /dev/null +++ b/code_study/Baekjoon/java/_1647_2.java @@ -0,0 +1,71 @@ +import java.util.*; + +class Node implements Comparable { + int node; + int dist; + + Node(int node, int dist) { + this.node = node; + this.dist = dist; + } + + @Override + public int compareTo(Node other) { + return this.dist - other.dist; + } +} + +public class _1647_2 { + static void prim(ArrayList> graph, int V) { + boolean[] visited = new boolean[V+1]; + PriorityQueue pq = new PriorityQueue<>(); + pq.add(new Node(1, 0)); + + int cnt = 0, total_dist = 0, max_dist = 0; + + while(!pq.isEmpty()) { + Node now = pq.poll(); + int current_node = now.node, current_dist = now.dist; + + if(visited[current_node]) continue; + + visited[current_node] = true; + total_dist += current_dist; + max_dist = Math.max(max_dist, current_dist); + cnt += 1; + + if(cnt == V) break; + + for(Node next : graph.get(current_node)) { + if(!visited[next.node]) { + pq.add(next); + } + } + } + + System.out.println(total_dist - max_dist); + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int N = sc.nextInt(), M = sc.nextInt(); + + if(N==2) { + System.out.println(0); + sc.close(); + return ; + } + + ArrayList> graph = new ArrayList<>(); + for(int i=0; i<=N; i++) graph.add(new ArrayList<>()); + + for(int i=0; i