From 17427e3f67790f0a7f27196127ae181f495593e2 Mon Sep 17 00:00:00 2001 From: oncsr Date: Sat, 14 Jun 2025 20:58:04 +0900 Subject: [PATCH] =?UTF-8?q?[20250614]=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=B6=8C?= =?UTF-8?q?=ED=98=81=EC=A4=80?= 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" | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 "khj20006/202506/14 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/khj20006/202506/14 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/khj20006/202506/14 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..5641701a --- /dev/null +++ "b/khj20006/202506/14 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,116 @@ +```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); + } + +} + +public class Main { + + static IOController io; + + // + + static int N, M; + static int[] r; + static long[] c; + + public static void main(String[] args) throws Exception { + + io = new IOController(); + + while(true) { + N = io.nextInt(); + M = io.nextInt(); + if(N == 0) break; + init(); + solve(); + } + + io.close(); + + } + + static int f(int x) { + if(x == r[x]) return x; + int p = f(r[x]); + c[x] += c[r[x]]; + return (r[x] = p); + } + + public static void init() throws Exception { + + r = new int[N+1]; + for(int i=1;i<=N;i++) r[i] = i; + c = new long[N+1]; + + } + + static void solve() throws Exception { + + while(M-->0) { + + char op = io.nextToken().charAt(0); + int a = io.nextInt(), b = io.nextInt(); + + if(op == '!') { + int w = io.nextInt(); + int x = f(a), y = f(b); + if(x == y) continue; + c[y] = c[a]-c[b]+w; + r[y] = x; + } + else { + int x = f(a), y = f(b); + if(x != y) io.write("UNKNOWN\n"); + else io.write((c[b] - c[a]) + "\n"); + } + + } + + } + +} +```