#include <cstdio> #include <vector> #include <cmath> #include <algorithm> #define MAXN 200005 #define MAXK 500005 #define LOGN (ceil(log(n))) using namespace std; int n,m,k; int p[MAXN][50],M[MAXN]; struct col { int a,b,val; col(int a,int b,int val) : a(a), b(b), val(val){} bool operator < (const col &c) const { return val < c.val; } }; struct tree { tree *parent; vector<tree*> next; vector<col> C; int f, id, pre, size; } T[MAXN]; void p_array(int root) { for(int i=1;i<=n;i++) if(T[i].parent != NULL) p[i][0] = T[i].parent->id; p[root][0] = root; for(int i=1;i<=LOGN;i++) for(int k=1;k<=n;k++) p[k][i] = p[p[k][i-1]][i-1]; } int pre_array(tree *root, int &pos) { root->pre = ++pos; root->size = 1; for(int i=0;i<root->next.size();i++) { if(root->next[i] == root->parent) continue; root->next[i]->parent = root; root->size += pre_array(root->next[i], pos); } return root->size; } bool descendant(tree *u, tree *v) { if(u->pre > v->pre && u->pre <= v->pre + v->size-1) return true; else return false; } tree* lca(tree *u, tree *v) { if(u == v) return u; if(descendant(u,v)) return v; if(descendant(v,u)) return u; int i=u->id, j=LOGN; while(j>=0) { if(descendant(v, &T[p[i][j]])) j=j-1; else i = p[i][j]; } return &T[p[i][0]]; } bool test(int a, tree* root) { if(T[a].id == root->id) return true; return descendant(&T[a],root); } long long int sim(tree* root) { long long int res = 0; sort(root->C.begin(), root->C.end()); int j=0,i=0; while( j<root->next.size() && i<root->C.size() ) { if(root->C[i].val < root->next[j]->pre) { int tmp1 = root->C[i].a; int tmp2 = root->C[i].b; int d = min(T[tmp1].f, T[tmp2].f); T[tmp1].f -= d; T[tmp2].f -= d; res += 2*d; i++; } else { res += sim(root->next[j]); j++; } } for(;j<root->next.size();j++) res += sim(root->next[j]); for(;i<root->C.size();i++) { int tmp1 = root->C[i].a; int tmp2 = root->C[i].b; int d = min(T[tmp1].f, T[tmp2].f); T[tmp1].f -= d; T[tmp2].f -= d; res += 2*d; } return res; } int main() { scanf("%d %d %d",&n,&m,&k); for(int i=1;i<=n;i++) { scanf("%d",&T[i].f); T[i].id = i; T[i].parent = NULL; } int x,y; for(int i=0;i<m;i++) { scanf("%d %d",&x,&y); T[x].parent = &T[y]; T[y].next.push_back(&T[x]); } tree *root = &T[y]; p_array(root->id); int pos = 1; pre_array(root, pos); int a,b; for(int i=0;i<k;i++) { scanf("%d %d",&a, &b); if(test(a,root) && test(b,root)) { tree *tmp = lca(&T[a], &T[b]); int val = max(T[a].pre, T[b].pre); tmp->C.push_back(col(a,b,val)); } } printf("%lld\n",sim(root)); 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 | #include <cstdio> #include <vector> #include <cmath> #include <algorithm> #define MAXN 200005 #define MAXK 500005 #define LOGN (ceil(log(n))) using namespace std; int n,m,k; int p[MAXN][50],M[MAXN]; struct col { int a,b,val; col(int a,int b,int val) : a(a), b(b), val(val){} bool operator < (const col &c) const { return val < c.val; } }; struct tree { tree *parent; vector<tree*> next; vector<col> C; int f, id, pre, size; } T[MAXN]; void p_array(int root) { for(int i=1;i<=n;i++) if(T[i].parent != NULL) p[i][0] = T[i].parent->id; p[root][0] = root; for(int i=1;i<=LOGN;i++) for(int k=1;k<=n;k++) p[k][i] = p[p[k][i-1]][i-1]; } int pre_array(tree *root, int &pos) { root->pre = ++pos; root->size = 1; for(int i=0;i<root->next.size();i++) { if(root->next[i] == root->parent) continue; root->next[i]->parent = root; root->size += pre_array(root->next[i], pos); } return root->size; } bool descendant(tree *u, tree *v) { if(u->pre > v->pre && u->pre <= v->pre + v->size-1) return true; else return false; } tree* lca(tree *u, tree *v) { if(u == v) return u; if(descendant(u,v)) return v; if(descendant(v,u)) return u; int i=u->id, j=LOGN; while(j>=0) { if(descendant(v, &T[p[i][j]])) j=j-1; else i = p[i][j]; } return &T[p[i][0]]; } bool test(int a, tree* root) { if(T[a].id == root->id) return true; return descendant(&T[a],root); } long long int sim(tree* root) { long long int res = 0; sort(root->C.begin(), root->C.end()); int j=0,i=0; while( j<root->next.size() && i<root->C.size() ) { if(root->C[i].val < root->next[j]->pre) { int tmp1 = root->C[i].a; int tmp2 = root->C[i].b; int d = min(T[tmp1].f, T[tmp2].f); T[tmp1].f -= d; T[tmp2].f -= d; res += 2*d; i++; } else { res += sim(root->next[j]); j++; } } for(;j<root->next.size();j++) res += sim(root->next[j]); for(;i<root->C.size();i++) { int tmp1 = root->C[i].a; int tmp2 = root->C[i].b; int d = min(T[tmp1].f, T[tmp2].f); T[tmp1].f -= d; T[tmp2].f -= d; res += 2*d; } return res; } int main() { scanf("%d %d %d",&n,&m,&k); for(int i=1;i<=n;i++) { scanf("%d",&T[i].f); T[i].id = i; T[i].parent = NULL; } int x,y; for(int i=0;i<m;i++) { scanf("%d %d",&x,&y); T[x].parent = &T[y]; T[y].next.push_back(&T[x]); } tree *root = &T[y]; p_array(root->id); int pos = 1; pre_array(root, pos); int a,b; for(int i=0;i<k;i++) { scanf("%d %d",&a, &b); if(test(a,root) && test(b,root)) { tree *tmp = lca(&T[a], &T[b]); int val = max(T[a].pre, T[b].pre); tmp->C.push_back(col(a,b,val)); } } printf("%lld\n",sim(root)); return 0; } |