-
Notifications
You must be signed in to change notification settings - Fork 1
/
1527.cpp
64 lines (51 loc) · 1.15 KB
/
1527.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<bits/stdc++.h>
using namespace std;
#define IO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl '\n'
const int mx = 1e5 + 10;
int p[mx];
int score[mx];
void init_set() {
for (int i = 0; i < mx; ++i) p[i] = i;
}
int find_set (int x) {
return p[x] == x ? p[x] : p[x] = find_set(p[x]);
}
void join (int x, int y) {
p[find_set(x)] = find_set(y);
int sum = score[x] + score[y];
score[x] = score[y] = sum;
}
int main() { IO
int n, m;
while (cin >> n >> m) {
if (n + m == 0) break;
init_set();
for (int i = 1; i <= n; ++i) cin >> score[i];
int ans = 0;
int q, a, b;
for (int i = 0; i < m; ++i) {
cin >> q >> a >> b;
if (q == 1) {
int s = find_set(a);
if (s == 1) swap (a, b);
join(a, b);
}
else {
int sc1 = score[find_set(a)];
int sc2 = score[find_set(b)];
if (sc1 == sc2) continue;
if (sc1 > sc2) {
int s = find_set(a);
if (s == 1) ans ++;
}
else if (sc2 > sc1) {
int s = find_set(b);
if (s == 1) ans ++;
}
}
}
cout << ans << endl;
}
return 0;
}