From 8ded16e30bc7ad0b786ddd6c766fcf068c1e2b52 Mon Sep 17 00:00:00 2001 From: zinnnn37 Date: Sun, 19 Oct 2025 21:52:17 +0900 Subject: [PATCH] =?UTF-8?q?[20251019]=20BOJ=20/=20P3=20/=20=EA=B5=90?= =?UTF-8?q?=EC=88=98=EB=8B=98=EC=9D=80=20=EA=B8=B0=EB=8B=A4=EB=A6=AC?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=EB=8B=A4=20/=20=EA=B9=80?= =?UTF-8?q?=EB=AF=BC=EC=A7=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0 \354\225\212\353\212\224\353\213\244.md" | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 "zinnnn37/202510/19 BOJ P3 \352\265\220\354\210\230\353\213\230\354\235\200 \352\270\260\353\213\244\353\246\254\354\247\200 \354\225\212\353\212\224\353\213\244.md" diff --git "a/zinnnn37/202510/19 BOJ P3 \352\265\220\354\210\230\353\213\230\354\235\200 \352\270\260\353\213\244\353\246\254\354\247\200 \354\225\212\353\212\224\353\213\244.md" "b/zinnnn37/202510/19 BOJ P3 \352\265\220\354\210\230\353\213\230\354\235\200 \352\270\260\353\213\244\353\246\254\354\247\200 \354\225\212\353\212\224\353\213\244.md" new file mode 100644 index 00000000..6d23ab14 --- /dev/null +++ "b/zinnnn37/202510/19 BOJ P3 \352\265\220\354\210\230\353\213\230\354\235\200 \352\270\260\353\213\244\353\246\254\354\247\200 \354\225\212\353\212\224\353\213\244.md" @@ -0,0 +1,93 @@ +```java +import java.io.*; +import java.util.StringTokenizer; + +public class BJ_3830_교수님은_기다리지_않는다 { + + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static final StringBuilder sb = new StringBuilder(); + private static StringTokenizer st; + + private static int N, M; + private static int[] parent, weight; + + public static void main(String[] args) throws IOException { + while (init()) { + sol(); + } + bw.flush(); + bw.close(); + br.close(); + } + + private static boolean init() throws IOException { + st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + if (N == 0) return false; + + parent = new int[N + 1]; + weight = new int[N + 1]; + for (int i = 1; i <= N; i++) { + parent[i] = i; + } + + return true; + } + + private static void sol() throws IOException { + while (M-- > 0) { + st = new StringTokenizer(br.readLine()); + + String cmd = st.nextToken(); + + if (cmd.equals("!")) { + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + int w = Integer.parseInt(st.nextToken()); + + union(a, b, w); + } else if (cmd.equals("?")) { + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + + if (find(a) == find(b)) { + bw.write(weight[b] - weight[a] + "\n"); + } else { + bw.write("UNKNOWN\n"); + } + } + } + } + + private static int find(int a) { + if (a == parent[a]) return a; + + int root = find(parent[a]); + weight[a] += weight[parent[a]]; + parent[a] = root; + + return root; + } + + private static void union(int a, int b, int w) { + int rootA = find(a); + int rootB = find(b); + + if (rootA == rootB) { + return; + } + + if (rootA < rootB) { + parent[rootB] = rootA; + weight[rootB] = weight[a] - weight[b] + w; + } else { + parent[rootA] = rootB; + weight[rootA] = weight[b] - weight[a] - w; + } + } + +} +``` \ No newline at end of file