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
| #include <bits/stdc++.h> #define int long long using namespace std;
const int N = 2e5 + 66;
int to[N << 1], nex[N << 1], head[N], cnt;
inline void add_edge(int x, int y) { to[++ cnt] = y; nex[cnt] = head[x]; return (void)(head[x] = cnt); }
vector <int> v[N]; int n, m, L, tot; int vis[N], val[N], w[N];
inline void dfs(int x, int num) { int i, y; w[num] += val[x], v[num].push_back(x); vis[x] = 1; for (i = head[x]; i; i = nex[i]) { y = to[i]; if (vis[y]) continue; dfs(y, num); } return; }
inline int fuck(int x) { sort(v[x].begin(), v[x].end()); int s = v[x].size(), ret(0), i, y; for (i = 0; i < s; ++ i) { y = v[x][i]; val[y] = 1; } w[x] -= s; for (i = s - 1; i >= 0; -- i) { y = v[x][i]; val[y] += min(L - 1, w[x]); if (val[y] != L) w[x] = 0; else w[x] -= (L - 1); if (w[x] <= 0) break; } for (i = 0; i < s; ++ i) { y = v[x][i]; ret = ret + y * val[y]; } return ret; }
signed main() { int i, j, x, y, res(0); n = read(), m = read(), L = read(); for (i = 1; i <= n; ++ i) val[i] = read(); for (i = 1; i <= m; ++ i) { x = read(), y = read(); add_edge(x, y), add_edge(y, x); } for (i = 1; i <= n; ++ i) if (! vis[i]) ++ tot, dfs(i, tot); for (i = 1; i <= tot; ++ i) res += fuck(i); put(res); return 0; }
|