From 192de6202a515220a9aa3685eb1fbf2db6742c07 Mon Sep 17 00:00:00 2001 From: oncsr Date: Tue, 29 Jul 2025 09:31:05 +0900 Subject: [PATCH] =?UTF-8?q?[20250729]=20BOJ=20/=20P5=20/=20=EB=B3=84?= =?UTF-8?q?=EC=9E=90=EB=A6=AC=20/=20=EA=B6=8C=ED=98=81=EC=A4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5 \353\263\204\354\236\220\353\246\254.md" | 215 ++++++++++++++++++ 1 file changed, 215 insertions(+) create mode 100644 "khj20006/202507/29 BOJ P5 \353\263\204\354\236\220\353\246\254.md" diff --git "a/khj20006/202507/29 BOJ P5 \353\263\204\354\236\220\353\246\254.md" "b/khj20006/202507/29 BOJ P5 \353\263\204\354\236\220\353\246\254.md" new file mode 100644 index 00000000..cded00d6 --- /dev/null +++ "b/khj20006/202507/29 BOJ P5 \353\263\204\354\236\220\353\246\254.md" @@ -0,0 +1,215 @@ +```java +import java.util.*; +import java.io.*; + +class IOController { + BufferedReader br; + BufferedWriter bw; + StringTokenizer st; + + public IOController() { + br = new BufferedReader(new InputStreamReader(System.in)); + bw = new BufferedWriter(new OutputStreamWriter(System.out)); + st = new StringTokenizer(""); + } + + String nextLine() throws Exception { + String line = br.readLine(); + st = new StringTokenizer(line); + return line; + } + + String nextToken() throws Exception { + while (!st.hasMoreTokens()) nextLine(); + return st.nextToken(); + } + + int nextInt() throws Exception { + return Integer.parseInt(nextToken()); + } + + long nextLong() throws Exception { + return Long.parseLong(nextToken()); + } + + double nextDouble() throws Exception { + return Double.parseDouble(nextToken()); + } + + void close() throws Exception { + bw.flush(); + bw.close(); + } + + void write(String content) throws Exception { + bw.write(content); + } + +} + +enum Status { + BEFORE, LINE, CIRCLE, AFTER +} + +public class Main { + + static IOController io; + + // + + static int N, M; + static int[][] edges; + static int[] r; + static Status[] s; + static List[] graph; + + public static void main(String[] args) throws Exception { + + io = new IOController(); + + init(); + solve(); + + io.close(); + + } + + static int f(int x) {return x==r[x] ? x : (r[x]=f(r[x]));} + + static void init() throws Exception { + + N = io.nextInt(); + M = io.nextInt(); + edges = new int[M][]; + for(int i=0;i(); + } + + } + + static void solve() throws Exception { + + Arrays.sort(edges, (a,b) -> b[2]-a[2]); + + int ans = edges[0][2], val = Integer.MAX_VALUE, idx = 0; + int lines = 0, circles = 0; + // weight sweeping + while(idx < M) { + int currentWeight = edges[idx][2]; + while(idx < M && edges[idx][2] == currentWeight) { + int a = edges[idx][0], b = edges[idx][1]; + int x = f(a), y = f(b); + if(x == y) { + if(s[x] == Status.CIRCLE) { + circles--; + s[x] = Status.AFTER; + } + else if(s[x] == Status.LINE) { + lines--; + if(graph[a].size() > 1 || graph[b].size() > 1) { + s[x] = Status.AFTER; + } + else { + circles++; + s[x] = Status.CIRCLE; + } + } + } + else { + if(s[x] == Status.BEFORE) { + r[x] = y; + if(s[y] == Status.BEFORE) { + lines++; + s[y] = Status.LINE; + } + else if(s[y] == Status.LINE) { + if(graph[b].size() > 1) { + lines--; + s[y] = Status.AFTER; + } + } + else if(s[y] == Status.CIRCLE) { + circles--; + s[y] = Status.AFTER; + } + } + else if(s[x] == Status.LINE) { + r[y] = x; + if(s[y] == Status.BEFORE) { + if(graph[a].size() > 1) { + lines--; + s[x] = Status.AFTER; + } + } + else if(s[y] == Status.LINE) { + lines--; + if(graph[a].size() > 1 || graph[b].size() > 1) { + lines--; + s[x] = Status.AFTER; + } + } + else if(s[y] == Status.CIRCLE) { + circles--; + lines--; + s[x] = Status.AFTER; + } + else { + lines--; + s[x] = Status.AFTER; + } + } + else if(s[x] == Status.CIRCLE) { + r[y] = x; + if(s[y] == Status.BEFORE) { + circles--; + s[x] = Status.AFTER; + } + else if(s[y] == Status.LINE) { + lines--; + circles--; + s[x] = Status.AFTER; + } + else if(s[y] == Status.CIRCLE) { + circles-=2; + s[x] = Status.AFTER; + } + else { + circles--; + s[x] = Status.AFTER; + } + } + else { + r[y] = x; + if(s[y] == Status.LINE) { + lines--; + } + else if(s[y] == Status.CIRCLE) { + circles--; + } + } + graph[a].add(b); + graph[b].add(a); + } + idx++; + } + + int res = Math.abs(lines-circles); + if(res <= val) { + ans = currentWeight; + val = res; + } + + } + + io.write(ans + " " + val); + + } + +} +```