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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <vector> #include <set> #include <map> #include <queue> #define DD double #define LL long long #define MAXN 500000 #define MAXM 500000 using namespace std; struct edg { int from, to, next; } b[MAXM * 2 + 5], ob[MAXM + 5], nb[MAXM * 10 + 5]; int g[MAXN + 5], cntb, n, m, k, group[MAXN + 5], dis[MAXN + 5], cntg, cnte, belong[MAXN + 5], ndis[MAXN * 2 + 5]; int ng[MAXN * 2 + 5], cntn; LL ans; bool ise[MAXN + 5], chan[MAXN + 5]; map<int, int> bh[MAXN + 5]; map<int, int> xbh[MAXN + 5]; set<int> ce[MAXN + 5]; vector<int> bigb[MAXM + 5]; vector<int> ning[MAXN + 5]; void ade(int fr, int to) { b[++cntb] = {fr, to, g[fr]}; g[fr] = cntb; } void ade2(int fr, int to) { nb[++cntn] = {fr, to, ng[fr]}; ng[fr] = cntn; } bool ok; void dfs(int now) { for (int i = g[now]; i; i = b[i].next) { if (group[b[i].to] != group[now]) continue; if (dis[b[i].to] == -1) dis[b[i].to] = dis[now] ^ 1, belong[b[i].to] = belong[now], dfs(b[i].to); else if (dis[b[i].to] != (dis[now] ^ 1)) { ok = false; return ; } if (!ok) return ; } } void dfs2(int now) { for (int i = ng[now]; i; i = nb[i].next) { if (ndis[nb[i].to] == -1) ndis[nb[i].to] = ndis[now] ^ 1, dfs2(nb[i].to); else if (ndis[nb[i].to] != (ndis[now] ^ 1)) { ok = false; return ; } if (!ok) return ; } } void recov(int now) { ndis[now] = -1; for (int i = ng[now]; i; i = nb[i].next) if (~ndis[nb[i].to]) recov(nb[i].to); } bool pd(int now) { ok = true; for (int i = 0; i < bigb[now].size(); i++) { int xi = ob[bigb[now][i]].from, yi = ob[bigb[now][i]].to; if (!ise[group[xi]] || !ise[group[yi]]) return false; int bx = belong[xi], by = belong[yi]; ade2(bx + dis[xi] * MAXN, by + dis[yi] * MAXN); ade2(by + dis[yi] * MAXN, bx + dis[xi] * MAXN); ade2(bx, bx + MAXN), ade2(bx + MAXN, bx); ade2(by, by + MAXN), ade2(by + MAXN, by); } for (int i = 0; i < bigb[now].size(); i++) { int xi = ob[bigb[now][i]].from, yi = ob[bigb[now][i]].to; int bx = belong[xi], by = belong[yi]; if (ndis[bx + dis[xi] * MAXN] == -1) ndis[bx + dis[xi] * MAXN] = 0, dfs2(bx + dis[xi] * MAXN); if (ndis[by + dis[yi] * MAXN] == -1) ndis[by + dis[yi] * MAXN] = 0, dfs2(by + dis[yi] * MAXN); } for (int i = 0; i < bigb[now].size(); i++) { int xi = ob[bigb[now][i]].from, yi = ob[bigb[now][i]].to; int bx = belong[xi], by = belong[yi]; if (~ndis[bx + dis[xi] * MAXN]) recov(bx + dis[xi] * MAXN); if (~ndis[by + dis[yi] * MAXN]) recov(by + dis[yi] * MAXN); } for (int i = 0; i < bigb[now].size(); i++) { int xi = ob[bigb[now][i]].from, yi = ob[bigb[now][i]].to; int bx = belong[xi], by = belong[yi]; ng[bx + MAXN] = ng[bx] = ng[by] = ng[by + MAXN] = 0; } cntn = 0; return ok; } int main() { memset(dis, -1, sizeof(dis)); memset(ndis, -1, sizeof(ndis)); scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; i++) { scanf("%d", &group[i]); ning[group[i]].push_back(i); } for (int i = 1, xi, yi; i <= m; i++) { scanf("%d%d", &xi, &yi); ade(xi, yi), ade(yi, xi); if (group[xi] > group[yi]) swap(xi, yi); ob[i].from = xi, ob[i].to = yi; int gx = group[xi], gy = group[yi]; if (gx == gy) continue; if (bh[gx].find(gy) == bh[gx].end()) bh[gx][gy] = ++cntg; bigb[bh[gx][gy]].push_back(i); } for (int i = 1; i <= k; i++) { ise[i] = true; if (ning[i].size()) { for (int j = 0; j < ning[i].size(); j++) if (dis[ning[i][j]] == -1) { ok = true; dis[ning[i][j]] = 0; belong[ning[i][j]] = ++belong[0]; dfs(ning[i][j]); if (!ok) ise[i] = false; } } if (ise[i]) ++cnte; } for (int i = 1, xi, yi; i <= m; i++) { xi = ob[i].from, yi = ob[i].to; int gx = group[xi], gy = group[yi]; if (!ise[gx] || !ise[gy] || gx == gy) continue; ce[gx].insert(gy), ce[gy].insert(gx); } if (cnte) { for (int i = 1; i <= k; i++) if (ise[i]) ans += cnte - 1 - (int)ce[i].size(); } ans >>= 1; for (int i = 1; i <= cntg; i++) if (pd(i)) ++ans; printf("%lld", ans); return 0; }
|