Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions zinnnn37/202510/19 BOJ P3 교수님은 기다리지 않는다.md
Original file line number Diff line number Diff line change
@@ -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;
}
}

}
```