#include <cstdio> #include <algorithm> #include <vector> #define MAXN 200003 #define MAXK 500003 typedef long long LL; typedef std::vector<int> VI; struct FindAndUnion { int p[MAXN],r[MAXN]; int F(int x); void U(int x,int y); }FNU; int n,m,k; int g[MAXN],depth[MAXN]; int sub_size[MAXN]; bool pos_in_deg[MAXN]; bool finished[MAXN]; VI G[MAXN],ENQ[MAXN],roots; int F[MAXN],P[MAXN],ANC[MAXN],Q[MAXN],rt[MAXN]; int c[MAXK],d[MAXK],order[MAXK]; int p_lvl[MAXK],p_edge[MAXK]; LL result; void T(int root); int sec(int x,int i); int main() { scanf("%d%d%d",&n,&m,&k); for(int i = 1; i <= n; ++i) scanf("%d",g+i),F[i] = -1; for(int i = 1,a,b; i <= m; ++i) scanf("%d%d",&a,&b),G[b].push_back(a),pos_in_deg[a] = 1; for(int i = 1; i <= n; ++i) if(!pos_in_deg[i]) roots.push_back(i); for(int i = 1; i <= k; ++i) scanf("%d%d",c+i,d+i),ENQ[c[i]].push_back(i),ENQ[d[i]].push_back(i),order[i] = i,p_lvl[i] = p_edge[i] = -1; for(int i = 1; i <= n; ++i) std::reverse(G[i].begin(),G[i].end()); for(int root : roots) T(root); std::sort(order+1,order+k+1,[&](int i,int j)->bool { if(p_lvl[i] == p_lvl[j]) { if(p_edge[i] == p_edge[j]) return i < j; return p_edge[i] > p_edge[j]; } return p_lvl[i] > p_lvl[j]; }); for(int i = 1,r_i; i <= k; ++i) { if(p_lvl[r_i = order[i]] == -1) continue; int x = std::min(g[c[r_i]],g[d[r_i]]); g[c[r_i]] -= x; g[d[r_i]] -= x; result += (LL)(x); } printf("%lld",result<<1); return 0; } void T(int root) { int q,v,u; Q[q = 1] = root; ANC[root] = root; F[root] = G[root].size(); P[root] = root; depth[root] = 0; rt[root] = root; while(q) { if(F[v = Q[q]]) { u = G[v][--F[v]]; Q[++q] = u; ANC[u] = u; F[u] = G[u].size(); P[u] = v; depth[u] = depth[v] + 1; rt[u] = root; } else { for(int enq : ENQ[v]) if(rt[v] == rt[u = sec(v,enq)]) { if(finished[u]) { p_lvl[enq] = depth[ANC[FNU.F(u)]]; if(p_edge[enq] == -1) p_edge[enq] = F[ANC[FNU.F(u)]]; } else p_edge[enq] = F[u]; } FNU.U(P[v],v); ANC[FNU.F(v)] = P[v]; finished[v] = 1; --q; } } } int FindAndUnion::F(int x) { return p[x] == 0 ? x : p[x] = F(p[x]); } void FindAndUnion::U(int x,int y) { x = F(x); y = F(y); if(r[x] > r[y]) p[y] = x; else if(r[x] < r[y]) p[x] = y; else if(x != y) { ++r[x]; p[y] = x; } } inline int sec(int x,int i) { return x == c[i] ? d[i] : c[i]; }
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 | #include <cstdio> #include <algorithm> #include <vector> #define MAXN 200003 #define MAXK 500003 typedef long long LL; typedef std::vector<int> VI; struct FindAndUnion { int p[MAXN],r[MAXN]; int F(int x); void U(int x,int y); }FNU; int n,m,k; int g[MAXN],depth[MAXN]; int sub_size[MAXN]; bool pos_in_deg[MAXN]; bool finished[MAXN]; VI G[MAXN],ENQ[MAXN],roots; int F[MAXN],P[MAXN],ANC[MAXN],Q[MAXN],rt[MAXN]; int c[MAXK],d[MAXK],order[MAXK]; int p_lvl[MAXK],p_edge[MAXK]; LL result; void T(int root); int sec(int x,int i); int main() { scanf("%d%d%d",&n,&m,&k); for(int i = 1; i <= n; ++i) scanf("%d",g+i),F[i] = -1; for(int i = 1,a,b; i <= m; ++i) scanf("%d%d",&a,&b),G[b].push_back(a),pos_in_deg[a] = 1; for(int i = 1; i <= n; ++i) if(!pos_in_deg[i]) roots.push_back(i); for(int i = 1; i <= k; ++i) scanf("%d%d",c+i,d+i),ENQ[c[i]].push_back(i),ENQ[d[i]].push_back(i),order[i] = i,p_lvl[i] = p_edge[i] = -1; for(int i = 1; i <= n; ++i) std::reverse(G[i].begin(),G[i].end()); for(int root : roots) T(root); std::sort(order+1,order+k+1,[&](int i,int j)->bool { if(p_lvl[i] == p_lvl[j]) { if(p_edge[i] == p_edge[j]) return i < j; return p_edge[i] > p_edge[j]; } return p_lvl[i] > p_lvl[j]; }); for(int i = 1,r_i; i <= k; ++i) { if(p_lvl[r_i = order[i]] == -1) continue; int x = std::min(g[c[r_i]],g[d[r_i]]); g[c[r_i]] -= x; g[d[r_i]] -= x; result += (LL)(x); } printf("%lld",result<<1); return 0; } void T(int root) { int q,v,u; Q[q = 1] = root; ANC[root] = root; F[root] = G[root].size(); P[root] = root; depth[root] = 0; rt[root] = root; while(q) { if(F[v = Q[q]]) { u = G[v][--F[v]]; Q[++q] = u; ANC[u] = u; F[u] = G[u].size(); P[u] = v; depth[u] = depth[v] + 1; rt[u] = root; } else { for(int enq : ENQ[v]) if(rt[v] == rt[u = sec(v,enq)]) { if(finished[u]) { p_lvl[enq] = depth[ANC[FNU.F(u)]]; if(p_edge[enq] == -1) p_edge[enq] = F[ANC[FNU.F(u)]]; } else p_edge[enq] = F[u]; } FNU.U(P[v],v); ANC[FNU.F(v)] = P[v]; finished[v] = 1; --q; } } } int FindAndUnion::F(int x) { return p[x] == 0 ? x : p[x] = F(p[x]); } void FindAndUnion::U(int x,int y) { x = F(x); y = F(y); if(r[x] > r[y]) p[y] = x; else if(r[x] < r[y]) p[x] = y; else if(x != y) { ++r[x]; p[y] = x; } } inline int sec(int x,int i) { return x == c[i] ? d[i] : c[i]; } |