-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC.cpp
104 lines (103 loc) · 2.27 KB
/
C.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
int par[maxn], sub[maxn], col[maxn];
vector <pair <int, int>> parHis, colHis, subHis;
bool bad[maxn];
int c[maxn];
int root(int x) {
while(x != par[x]) {
x = par[x];
}
return x;
}
int getCol(int x) {
int res = 0;
while(x != par[x]) {
res ^= col[x];
x = par[x];
}
return res;
}
void save(int x, int y) {
parHis.emplace_back(x, par[x]);
subHis.emplace_back(y, sub[y]);
colHis.emplace_back(x, col[x]);
}
void join(int x, int y, bool rollback = false) {
int rx = root(x);
int ry = root(y);
int dif = (getCol(x) == getCol(y));
if(sub[rx] > sub[ry]) swap(rx, ry);
if(rollback) save(rx, ry);
par[rx] = ry;
sub[ry] += sub[rx];
col[rx] = dif;
}
bool check(int p, int q) {
if(root(p) == root(q) && getCol(p) == getCol(q)) return false;
return true;
}
int main() {
int n, m, k;
scanf("%d %d %d", &n, &m, &k);
for(int i = 1; i <= n; i++) {
scanf("%d", &c[i]);
}
map <pair <int, int>, vector <pair <int, int>>> cont;
for(int i = 1; i <= m; i++) {
int p, q;
scanf("%d %d", &p, &q);
if(c[p] > c[q]) swap(p, q);
cont[make_pair(c[p], c[q])].emplace_back(p, q);
}
for(int i = 1; i <= n; i++) {
par[i] = i;
col[i] = 0;
sub[i] = 1;
}
int active = k;
for(int i = 1; i <= k; i++) {
auto v = cont[make_pair(i, i)];
for(auto j : v) {
int p = j.first;
int q = j.second;
if(check(p, q)) {
join(p, q);
} else {
bad[i] = true;
active -= 1;
break;
}
}
}
long long ans = (1LL * active * (active - 1)) / 2;
for(auto i : cont) {
if(i.first.first == i.first.second) continue;
if(bad[i.first.first] || bad[i.first.second]) continue;
auto v = i.second;
for(auto j : v) {
int p = j.first;
int q = j.second;
if(check(p, q)) {
join(p, q, true);
} else {
ans -= 1;
break;
}
}
while(not parHis.empty()) {
auto ph = parHis.back();
auto ch = colHis.back();
auto sh = subHis.back();
parHis.pop_back();
colHis.pop_back();
subHis.pop_back();
par[ph.first] = ph.second;
col[ch.first] = ch.second;
sub[sh.first] = sh.second;
}
}
printf("%lld\n", ans);
return 0;
}