#include <bits/stdc++.h> #define FWD(a,b,c) for(int a=(b); a<(c); ++a) #define BCK(a,b,c) for(int a=(b); a>(c); --a) #define st first #define nd second using namespace std; typedef long long LL; typedef pair<int, int> PII; const int MAXN = 200010; const int MAXK = 500010; int n, m, k; LL res; int weight[MAXN]; int subsA[MAXK]; int subsB[MAXK]; int repr[MAXN]; int usd[MAXN]; bool vis[MAXN]; int act[MAXN]; int done[MAXK]; vector<int> reacts[MAXN]; vector<int> sons[MAXN]; vector<PII> what[MAXN]; inline int find(int u){ return repr[u]==u?u:repr[u]=find(repr[u]); } #define another(r, u) (subsA[r] == u ? subsB[r] : subsA[r]) void dfs(int u){ repr[u] = u; vis[u] = 1; for(int v : sons[u]){ ++act[u]; dfs(v); repr[v] = u; } for(int r : reacts[u]) if(!done[r] && vis[another(r, u)]){ done[r] = 1; //printf("wrzucam %d %d do %d (%d, %d)\n", subsA[r], subsB[r], find(another(r, u)), act[find(another(r, u))], r); what[find(another(r, u))].push_back(PII(act[find(another(r, u))], r)); } sort(what[u].begin(), what[u].end()); for(PII sr : what[u]){ int r = sr.nd; //printf("%d %d\n", subsA[r], subsB[r]); int sed = min(weight[subsA[r]], weight[subsB[r]]); res += 2 * sed; weight[subsA[r]] -= sed; weight[subsB[r]] -= sed; } } int main(){ scanf("%d %d %d", &n, &m, &k); FWD(i,1,n+1) scanf("%d", &weight[i]); FWD(i,0,m){ int a, b; scanf("%d %d", &a, &b); sons[b].push_back(a); usd[a] = 1; } FWD(i,0,k){ scanf("%d %d", &subsA[i], &subsB[i]); reacts[subsA[i]].push_back(i); reacts[subsB[i]].push_back(i); } FWD(i,1,n+1) if(!usd[i]) dfs(i); printf("%lld\n", res); 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 | #include <bits/stdc++.h> #define FWD(a,b,c) for(int a=(b); a<(c); ++a) #define BCK(a,b,c) for(int a=(b); a>(c); --a) #define st first #define nd second using namespace std; typedef long long LL; typedef pair<int, int> PII; const int MAXN = 200010; const int MAXK = 500010; int n, m, k; LL res; int weight[MAXN]; int subsA[MAXK]; int subsB[MAXK]; int repr[MAXN]; int usd[MAXN]; bool vis[MAXN]; int act[MAXN]; int done[MAXK]; vector<int> reacts[MAXN]; vector<int> sons[MAXN]; vector<PII> what[MAXN]; inline int find(int u){ return repr[u]==u?u:repr[u]=find(repr[u]); } #define another(r, u) (subsA[r] == u ? subsB[r] : subsA[r]) void dfs(int u){ repr[u] = u; vis[u] = 1; for(int v : sons[u]){ ++act[u]; dfs(v); repr[v] = u; } for(int r : reacts[u]) if(!done[r] && vis[another(r, u)]){ done[r] = 1; //printf("wrzucam %d %d do %d (%d, %d)\n", subsA[r], subsB[r], find(another(r, u)), act[find(another(r, u))], r); what[find(another(r, u))].push_back(PII(act[find(another(r, u))], r)); } sort(what[u].begin(), what[u].end()); for(PII sr : what[u]){ int r = sr.nd; //printf("%d %d\n", subsA[r], subsB[r]); int sed = min(weight[subsA[r]], weight[subsB[r]]); res += 2 * sed; weight[subsA[r]] -= sed; weight[subsB[r]] -= sed; } } int main(){ scanf("%d %d %d", &n, &m, &k); FWD(i,1,n+1) scanf("%d", &weight[i]); FWD(i,0,m){ int a, b; scanf("%d %d", &a, &b); sons[b].push_back(a); usd[a] = 1; } FWD(i,0,k){ scanf("%d %d", &subsA[i], &subsB[i]); reacts[subsA[i]].push_back(i); reacts[subsB[i]].push_back(i); } FWD(i,1,n+1) if(!usd[i]) dfs(i); printf("%lld\n", res); return 0; } |