#include <cstdio> #include <vector> #include <queue> #include <stack> #include <cstring> #include <iostream> #include <algorithm> #include <set> #define MAXN 200007 #define MAXK 500007 #define INF #define PB push_back #define MP make_pair #define ST first #define ND second #define REP(i,n) for(int i=0;i<(n);i++) #define FOR(a,b,c) for(int a=b;a<=(c);a++) #define FORD(a,b,c) for (int a=b;a>=(c);a--) #define VAR(v,n) __typeof(n) v=(n) #define ALL(c) c.begin(),c.end() #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();i++) using namespace std; typedef long long LL; typedef pair<int,int> PII; int n,m,k,a,b,czas; int father[MAXN],ojc[MAXN],wejscie[MAXN]; int c[MAXK],d[MAXK],ile_kraw[MAXN],gleb[MAXN]; LL ile[MAXN],res; vector<int> v[MAXN],sciezka; vector<PII> pyt[MAXN],wyn[MAXN]; bool bylo[MAXN]; queue<int> kolej; int find(int f) { if (father[f] == f) return f; father[f] = find(father[f]); return father[f]; } void dfs(int x) { bylo[x] = true; FOREACH(it,v[x]) { gleb[*it] = gleb[x] + 1; sciezka.PB(it-v[x].begin()); dfs(*it); sciezka.pop_back(); } FOREACH(it,pyt[x]) if (bylo[it -> ST]) { int pom = find(it -> ST); wyn[pom].PB(MP(sciezka[gleb[pom]],it -> ND)); } father[x] = ojc[x]; } int main(){ scanf("%d%d%d",&n,&m,&k); //printf("%d %d %d\n",n,m,k); FOR(i,1,n) scanf("%lld",&ile[i]); REP(i,m) { scanf("%d%d",&a,&b); v[b].PB(a); ojc[a] = b; } FOR(i,1,n) if (!ojc[i]) v[0].PB(i); REP(i,k) { scanf("%d%d",&c[i],&d[i]); pyt[c[i]].PB(MP(d[i],i)); pyt[d[i]].PB(MP(c[i],i)); } FOR(i,1,n) father[i] = i; dfs(0); FOR(i,0,n) ile_kraw[i] = int(v[i].size()); FOR(i,0,n) if (!ile_kraw[i]) kolej.push(i); REP(g,n) { int x = kolej.front(); kolej.pop(); sort(ALL(wyn[x])); FOREACH(it,wyn[x]) { LL pom = min(ile[c[it->ND]],ile[d[it->ND]]); res += pom*2; ile[c[it->ND]] -= pom; ile[d[it->ND]] -= pom; } --ile_kraw[ojc[x]]; if (!ile_kraw[ojc[x]]) kolej.push(ojc[x]); } 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #include <cstdio> #include <vector> #include <queue> #include <stack> #include <cstring> #include <iostream> #include <algorithm> #include <set> #define MAXN 200007 #define MAXK 500007 #define INF #define PB push_back #define MP make_pair #define ST first #define ND second #define REP(i,n) for(int i=0;i<(n);i++) #define FOR(a,b,c) for(int a=b;a<=(c);a++) #define FORD(a,b,c) for (int a=b;a>=(c);a--) #define VAR(v,n) __typeof(n) v=(n) #define ALL(c) c.begin(),c.end() #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();i++) using namespace std; typedef long long LL; typedef pair<int,int> PII; int n,m,k,a,b,czas; int father[MAXN],ojc[MAXN],wejscie[MAXN]; int c[MAXK],d[MAXK],ile_kraw[MAXN],gleb[MAXN]; LL ile[MAXN],res; vector<int> v[MAXN],sciezka; vector<PII> pyt[MAXN],wyn[MAXN]; bool bylo[MAXN]; queue<int> kolej; int find(int f) { if (father[f] == f) return f; father[f] = find(father[f]); return father[f]; } void dfs(int x) { bylo[x] = true; FOREACH(it,v[x]) { gleb[*it] = gleb[x] + 1; sciezka.PB(it-v[x].begin()); dfs(*it); sciezka.pop_back(); } FOREACH(it,pyt[x]) if (bylo[it -> ST]) { int pom = find(it -> ST); wyn[pom].PB(MP(sciezka[gleb[pom]],it -> ND)); } father[x] = ojc[x]; } int main(){ scanf("%d%d%d",&n,&m,&k); //printf("%d %d %d\n",n,m,k); FOR(i,1,n) scanf("%lld",&ile[i]); REP(i,m) { scanf("%d%d",&a,&b); v[b].PB(a); ojc[a] = b; } FOR(i,1,n) if (!ojc[i]) v[0].PB(i); REP(i,k) { scanf("%d%d",&c[i],&d[i]); pyt[c[i]].PB(MP(d[i],i)); pyt[d[i]].PB(MP(c[i],i)); } FOR(i,1,n) father[i] = i; dfs(0); FOR(i,0,n) ile_kraw[i] = int(v[i].size()); FOR(i,0,n) if (!ile_kraw[i]) kolej.push(i); REP(g,n) { int x = kolej.front(); kolej.pop(); sort(ALL(wyn[x])); FOREACH(it,wyn[x]) { LL pom = min(ile[c[it->ND]],ile[d[it->ND]]); res += pom*2; ile[c[it->ND]] -= pom; ile[d[it->ND]] -= pom; } --ile_kraw[ojc[x]]; if (!ile_kraw[ojc[x]]) kolej.push(ojc[x]); } printf("%lld\n",res); return 0; } |