//Krzysztof Kleiner #include<cstdio> #include<algorithm> #define FOR(i, b, n) for ( int (i) = b; (i) < (n); (i)++) #define FORR(i, b, n) for ( (i) = b; (i) < (n); (i)++) #define REP(i, n) for ( int (i) = 0; (i) < (n); (i)++) #define SC(n) scanf("%d", &(n)) #define SC2(n, m) scanf("%d%d", &(n), &(m)) #define SCLL(n) scanf("%lld", &(n)) #define PRT(n) printf("%d ", (n)) #define PRTLL(n) printf("%lld ", (n)) #define NXL printf("\n") typedef long long LL; typedef unsigned long long ULL; using namespace std; #include<vector> const int MANY = 400010; int parent[MANY]; int size[MANY], g[MANY], tree_parent[MANY]; int Find(int u) { if(parent[u] == u) return u; return parent[u] = Find(parent[u]); } inline void Union(int u, int v) { // if(size[v] < size[u]) // swap(u, v); parent[v] = u; // size[u] += size[u]; } struct node { vector<int> v, q; int left, right; }; node t[MANY]; int whereNow[MANY]; struct pii { int st, nd; }; pii os[500001]; int vis[MANY]; void lca_step(int u, int v); void make_lca(int u, int vnr) { if(u==0) return; parent[u] = u; size[u] = 1; if(t[u].left != 0) make_lca(t[u].left, vnr); if(t[u].right != 0) make_lca(t[u].right, vnr); vis[u] = vnr; for (int i=0; i<t[u].q.size(); i++) { pii pv = os [t[u].q[i]]; int v = pv.st; if(v == u) v = pv.nd; if (vis[v] == vnr) t [tree_parent[ Find(v) ]].v.push_back( t[u].q[i]); } if(t[u].left) Union(u, t[u].left); if(t[u].right) Union(u, t[u].right); } LL compute(int u) { if(u==0) return 0; LL res = 0; res += compute(t[u].left); res += compute(t[u].right); REP(i, t[u].v.size()) { pii para = os[t[u].v[i]]; int c = para.st; int d = para.nd; if(g[c] < g[d]) swap(c, d); g[c] -= g[d]; res += g[d]; g[d] = 0; } return res; } int main() { int n, m, k; SC2(n, m); SC(k); FOR(i, 1, n+1) { SC(g[i]); whereNow[i] = i; t[i].left = t[i].right = 0; tree_parent[i] = -1; } FOR(j, n+1, m+n+1) { tree_parent[j] = -1; int a, b; SC2(a, b); t[j].left = whereNow[a]; tree_parent[t[j].left] = j; t[j].right = whereNow[b]; tree_parent[t[j].right] = j; whereNow[b] = j; } REP(i, k) { SC2(os[i].st, os[i].nd); t[os[i].st] .q.push_back(i); t[os[i].nd] .q.push_back(i); } int vind = 1; FOR(i, 1, m+n+1) if(tree_parent[i] == -1) make_lca(i, vind++); LL res = 0; FOR(i, 1, m+n+1) if(tree_parent[i] == -1) res += compute(i); PRTLL(2*res); // printf("%lld\n", 2*res); NXL; return 0; }
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 | //Krzysztof Kleiner #include<cstdio> #include<algorithm> #define FOR(i, b, n) for ( int (i) = b; (i) < (n); (i)++) #define FORR(i, b, n) for ( (i) = b; (i) < (n); (i)++) #define REP(i, n) for ( int (i) = 0; (i) < (n); (i)++) #define SC(n) scanf("%d", &(n)) #define SC2(n, m) scanf("%d%d", &(n), &(m)) #define SCLL(n) scanf("%lld", &(n)) #define PRT(n) printf("%d ", (n)) #define PRTLL(n) printf("%lld ", (n)) #define NXL printf("\n") typedef long long LL; typedef unsigned long long ULL; using namespace std; #include<vector> const int MANY = 400010; int parent[MANY]; int size[MANY], g[MANY], tree_parent[MANY]; int Find(int u) { if(parent[u] == u) return u; return parent[u] = Find(parent[u]); } inline void Union(int u, int v) { // if(size[v] < size[u]) // swap(u, v); parent[v] = u; // size[u] += size[u]; } struct node { vector<int> v, q; int left, right; }; node t[MANY]; int whereNow[MANY]; struct pii { int st, nd; }; pii os[500001]; int vis[MANY]; void lca_step(int u, int v); void make_lca(int u, int vnr) { if(u==0) return; parent[u] = u; size[u] = 1; if(t[u].left != 0) make_lca(t[u].left, vnr); if(t[u].right != 0) make_lca(t[u].right, vnr); vis[u] = vnr; for (int i=0; i<t[u].q.size(); i++) { pii pv = os [t[u].q[i]]; int v = pv.st; if(v == u) v = pv.nd; if (vis[v] == vnr) t [tree_parent[ Find(v) ]].v.push_back( t[u].q[i]); } if(t[u].left) Union(u, t[u].left); if(t[u].right) Union(u, t[u].right); } LL compute(int u) { if(u==0) return 0; LL res = 0; res += compute(t[u].left); res += compute(t[u].right); REP(i, t[u].v.size()) { pii para = os[t[u].v[i]]; int c = para.st; int d = para.nd; if(g[c] < g[d]) swap(c, d); g[c] -= g[d]; res += g[d]; g[d] = 0; } return res; } int main() { int n, m, k; SC2(n, m); SC(k); FOR(i, 1, n+1) { SC(g[i]); whereNow[i] = i; t[i].left = t[i].right = 0; tree_parent[i] = -1; } FOR(j, n+1, m+n+1) { tree_parent[j] = -1; int a, b; SC2(a, b); t[j].left = whereNow[a]; tree_parent[t[j].left] = j; t[j].right = whereNow[b]; tree_parent[t[j].right] = j; whereNow[b] = j; } REP(i, k) { SC2(os[i].st, os[i].nd); t[os[i].st] .q.push_back(i); t[os[i].nd] .q.push_back(i); } int vind = 1; FOR(i, 1, m+n+1) if(tree_parent[i] == -1) make_lca(i, vind++); LL res = 0; FOR(i, 1, m+n+1) if(tree_parent[i] == -1) res += compute(i); PRTLL(2*res); // printf("%lld\n", 2*res); NXL; return 0; } |