#include <iostream> #include <algorithm> #include <cmath> typedef long long int ll; int n,m,k; int r; const int N = 200000, M = 200000, K = 500000; const int LGN = 20; int lgn; ll g[N+1]; struct node { node *left; node *right; node *parent; int v; int pre; int post; int s; }; node z[2*N+2]; node *p[LGN+4][2*N+2]; node *namieszalem[2*N+2]; node *root; void dfs(node *u,int &pre,int &post) { u->pre = ++pre; u->s ++; if(u->left) { dfs(u->left,pre,post); u->s += u->left->s; dfs(u->right,pre,post); u->s += u->right->s; } p[0][u-z] = u->parent; u->post = ++post; } void dfs() { r++; root = z+r; z[r].pre = 1; z[r].s = 1; int pre = 1; int post = 0; p[0][r] = root; for(int i=1;i<r;i++) if(!z[i].parent) { z[i].parent = root; dfs(z+i,pre,post); z[r].s += z[i].s; } z[r].post = ++post; } bool dzieciok(node*u,node*v) { return u->pre>=v->pre && u->pre<v->pre+v->s; } node *lca(node*u,node*v) { if(dzieciok(u,v)) return v; if(dzieciok(v,u)) return u; int j = lgn; while(j>=0) { if(dzieciok(v,p[j][u-z])) j --; else u = p[j][u-z]; } return p[0][u-z]; } struct reakcja { int a,b; int rank; }; reakcja R[K+1]; bool cmp(const reakcja&a,const reakcja&b) { return a.rank < b.rank; } int main() { std::cin.sync_with_stdio(false); std::cin >> n >> m >> k; lgn = int(std::log(2.0*n)/std::log(2.0)+2); for(int i=1;i<=n;i++) { std::cin >> g[i]; z[i].v = i; namieszalem[i] = z+i; } r = n; for(int i=1;i<=m;i++) { int a,b; std::cin >> a >> b; r++; z[r].left = namieszalem[a]; z[r].right = namieszalem[b]; namieszalem[a]->parent = z+r; namieszalem[b]->parent = z+r; z[r].v = r; namieszalem[b] = z+r; } dfs(); for(int i=1;i<=LGN;i++) for(int j=1;j<=r;j++) p[i][j] = p[i-1][p[i-1][j]-z]; for(int i=0;i<k;i++) { std::cin >> R[i].a >> R[i].b; R[i].rank = lca(z+R[i].a,z+R[i].b)->post; } std::stable_sort(R,R+k,cmp); ll osad = 0; for(int i=0;i<k;i++) { if(R[i].rank==r) continue; ll u = std::min(g[R[i].a],g[R[i].b]); osad += 2*u; g[R[i].a] -= u; g[R[i].b] -= u; } std::cout << osad << std::endl; 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 142 143 144 145 146 147 148 149 150 151 | #include <iostream> #include <algorithm> #include <cmath> typedef long long int ll; int n,m,k; int r; const int N = 200000, M = 200000, K = 500000; const int LGN = 20; int lgn; ll g[N+1]; struct node { node *left; node *right; node *parent; int v; int pre; int post; int s; }; node z[2*N+2]; node *p[LGN+4][2*N+2]; node *namieszalem[2*N+2]; node *root; void dfs(node *u,int &pre,int &post) { u->pre = ++pre; u->s ++; if(u->left) { dfs(u->left,pre,post); u->s += u->left->s; dfs(u->right,pre,post); u->s += u->right->s; } p[0][u-z] = u->parent; u->post = ++post; } void dfs() { r++; root = z+r; z[r].pre = 1; z[r].s = 1; int pre = 1; int post = 0; p[0][r] = root; for(int i=1;i<r;i++) if(!z[i].parent) { z[i].parent = root; dfs(z+i,pre,post); z[r].s += z[i].s; } z[r].post = ++post; } bool dzieciok(node*u,node*v) { return u->pre>=v->pre && u->pre<v->pre+v->s; } node *lca(node*u,node*v) { if(dzieciok(u,v)) return v; if(dzieciok(v,u)) return u; int j = lgn; while(j>=0) { if(dzieciok(v,p[j][u-z])) j --; else u = p[j][u-z]; } return p[0][u-z]; } struct reakcja { int a,b; int rank; }; reakcja R[K+1]; bool cmp(const reakcja&a,const reakcja&b) { return a.rank < b.rank; } int main() { std::cin.sync_with_stdio(false); std::cin >> n >> m >> k; lgn = int(std::log(2.0*n)/std::log(2.0)+2); for(int i=1;i<=n;i++) { std::cin >> g[i]; z[i].v = i; namieszalem[i] = z+i; } r = n; for(int i=1;i<=m;i++) { int a,b; std::cin >> a >> b; r++; z[r].left = namieszalem[a]; z[r].right = namieszalem[b]; namieszalem[a]->parent = z+r; namieszalem[b]->parent = z+r; z[r].v = r; namieszalem[b] = z+r; } dfs(); for(int i=1;i<=LGN;i++) for(int j=1;j<=r;j++) p[i][j] = p[i-1][p[i-1][j]-z]; for(int i=0;i<k;i++) { std::cin >> R[i].a >> R[i].b; R[i].rank = lca(z+R[i].a,z+R[i].b)->post; } std::stable_sort(R,R+k,cmp); ll osad = 0; for(int i=0;i<k;i++) { if(R[i].rank==r) continue; ll u = std::min(g[R[i].a],g[R[i].b]); osad += 2*u; g[R[i].a] -= u; g[R[i].b] -= u; } std::cout << osad << std::endl; return 0; } |