#include <cstdio> #include <vector> #include <algorithm> using namespace std; #define MAXN 200001 struct que{ int f; int s; int inc; int lca; int odd; }; int n,m,k; int weight[MAXN]; int inc[MAXN]; int color[MAXN]; int postOrd[MAXN]; int depth[MAXN]; int prevvv[MAXN]; int sstime[MAXN]; vector <int> G[MAXN]; vector <que> quer; vector <que> queries; vector <int> roots; int postOrdCount=0; long long int allWaste=0; bool queSorter(que a, que b){ if(sstime[a.lca]==sstime[b.lca]){ if(a.odd==b.odd){ if(a.inc<b.inc){ return true; }else{ return false; } }else if(a.odd<b.odd){ return true; }else{ return false; } }else if(sstime[a.lca]<sstime[b.lca]){ return true; }else{ return false; } } void readIn(){ scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=n;++i){ scanf("%d",&weight[i]); } int a,b; int t=0; for(int i=0;i<m;++i){ scanf("%d%d",&a,&b); G[b].push_back(a); t++; sstime[a]=t; inc[a]++; } que el; for(int i=0;i<k;++i){ scanf("%d%d",&el.f,&el.s); el.inc=i; quer.push_back(el); } for(int i=1;i<=n;++i){ if(inc[i]==0){ roots.push_back(i); } } } void dfs(int s, int col){ for(int i=0;i<G[s].size();++i){ int w=G[s][i]; //printf("-> %d %d\n",s,w); depth[w]=depth[s]+1; color[w]=col; prevvv[w]=s; dfs(w,col); } postOrdCount++; postOrd[s]=postOrdCount; } void startDfs(){ int coll=0; for(int i=0;i<roots.size();++i){ coll++; color[roots[i]]=coll; depth[roots[i]]=0; prevvv[roots[i]]=-1; dfs(roots[i],coll); } } int getLca(int a, int b){ while(depth[a]>depth[b]){ a=prevvv[a]; } while(depth[b]>depth[a]){ b=prevvv[b]; } while(a!=b){ a=prevvv[a]; b=prevvv[b]; } return a; } void filterQueries(){ que qq; for(int i=0;i<quer.size();++i){ if(color[quer[i].f]==color[quer[i].s]){ qq=quer[i]; qq.lca=getLca(qq.f,qq.s); qq.odd=max(depth[qq.f],depth[qq.s])-depth[qq.lca]; queries.push_back(qq); } } } void runMain(){ sort(queries.begin(),queries.end(),queSorter); int waste; for(int i=0;i<queries.size();++i){ waste=min(weight[queries[i].f],weight[queries[i].s]); weight[queries[i].f]-=waste; weight[queries[i].s]-=waste; allWaste+=2*waste; } } int main(){ readIn(); startDfs(); filterQueries(); runMain(); printf("%lld\n",allWaste); 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 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; #define MAXN 200001 struct que{ int f; int s; int inc; int lca; int odd; }; int n,m,k; int weight[MAXN]; int inc[MAXN]; int color[MAXN]; int postOrd[MAXN]; int depth[MAXN]; int prevvv[MAXN]; int sstime[MAXN]; vector <int> G[MAXN]; vector <que> quer; vector <que> queries; vector <int> roots; int postOrdCount=0; long long int allWaste=0; bool queSorter(que a, que b){ if(sstime[a.lca]==sstime[b.lca]){ if(a.odd==b.odd){ if(a.inc<b.inc){ return true; }else{ return false; } }else if(a.odd<b.odd){ return true; }else{ return false; } }else if(sstime[a.lca]<sstime[b.lca]){ return true; }else{ return false; } } void readIn(){ scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=n;++i){ scanf("%d",&weight[i]); } int a,b; int t=0; for(int i=0;i<m;++i){ scanf("%d%d",&a,&b); G[b].push_back(a); t++; sstime[a]=t; inc[a]++; } que el; for(int i=0;i<k;++i){ scanf("%d%d",&el.f,&el.s); el.inc=i; quer.push_back(el); } for(int i=1;i<=n;++i){ if(inc[i]==0){ roots.push_back(i); } } } void dfs(int s, int col){ for(int i=0;i<G[s].size();++i){ int w=G[s][i]; //printf("-> %d %d\n",s,w); depth[w]=depth[s]+1; color[w]=col; prevvv[w]=s; dfs(w,col); } postOrdCount++; postOrd[s]=postOrdCount; } void startDfs(){ int coll=0; for(int i=0;i<roots.size();++i){ coll++; color[roots[i]]=coll; depth[roots[i]]=0; prevvv[roots[i]]=-1; dfs(roots[i],coll); } } int getLca(int a, int b){ while(depth[a]>depth[b]){ a=prevvv[a]; } while(depth[b]>depth[a]){ b=prevvv[b]; } while(a!=b){ a=prevvv[a]; b=prevvv[b]; } return a; } void filterQueries(){ que qq; for(int i=0;i<quer.size();++i){ if(color[quer[i].f]==color[quer[i].s]){ qq=quer[i]; qq.lca=getLca(qq.f,qq.s); qq.odd=max(depth[qq.f],depth[qq.s])-depth[qq.lca]; queries.push_back(qq); } } } void runMain(){ sort(queries.begin(),queries.end(),queSorter); int waste; for(int i=0;i<queries.size();++i){ waste=min(weight[queries[i].f],weight[queries[i].s]); weight[queries[i].f]-=waste; weight[queries[i].s]-=waste; allWaste+=2*waste; } } int main(){ readIn(); startDfs(); filterQueries(); runMain(); printf("%lld\n",allWaste); return 0; } |