-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3830.cpp
50 lines (45 loc) · 875 Bytes
/
3830.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
char o;
int N, M, a, b;
int parent[100001];
ll cost[100001], w;
int find(int x) {
if(x == parent[x]) return x;
else {
int temp = find(parent[x]);
cost[x] += cost[parent[x]];
return parent[x] = temp;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
while(1) {
cin >> N >> M;
if(!N && !M) {return 0;}
for(int i = 1; i <= N; i++) {parent[i] = i; cost[i] = 0;}
for(int i = 0; i < M; i++){
cin >> o;
if(o == '!') {
cin >> a >> b >> w;
int l = find(a);
int r = find(b);
if(l != r){
parent[l] = r;
cost[l] = cost[b] - cost[a] + w;
}
}
else {
cin >> a >> b;
int l = find(a);
int r = find(b);
if(l != r) cout << "UNKNOWN\n";
else cout << cost[a]-cost[b] << "\n";
}
}
}
return 0;
}