#include <cstdio> #include <algorithm> #include <vector> #include <cassert> #define fru(j,n) for(int j=0;j<n;++j) #define tr(it,x) for(typeof(x.begin()) it=x.begin();it!=x.end();++it) #define x first #define y second using namespace std; typedef long long LL; typedef pair<int,int> pii; const int MAXN= 500005; int ILE[MAXN]; int D[MAXN][2]; int REP[MAXN],KTO[MAXN]; int REA[MAXN][2]; int find(int x) { return REP[x]==x?x:(REP[x]=find(REP[x])); } vector<pii> ZAP[MAXN]; vector<int> ROB[MAXN]; LL ret;int n; void dfs(int u) { // printf("dfs(%d)\n",u); tr(it,ZAP[u]) if(REP[it->x]!=-1) ROB[find(it->x)].push_back(it->y); REP[u]=u; if(u>=n) fru(j,2) { int u2=D[u][j]; // printf("z %d ide do %d\n",u,u2); dfs(u2); REP[u2]=u; assert(find(u)==u); } sort(ROB[u].begin(),ROB[u].end()); tr(it,ROB[u]) { int a=REA[*it][0],b=REA[*it][1], e=min(ILE[a],ILE[b]); ILE[a]-=e; ILE[b]-=e; ret+=e; } } int main() { int m,k; scanf("%d%d%d",&n,&m,&k); fru(i,n) scanf("%d",&ILE[i]); int u=n; fru(i,n) KTO[i]=i; fru(i,m) { int a,b; scanf("%d%d",&a,&b); --a;--b; D[u][0]=KTO[a]; D[u][1]=KTO[b]; KTO[b]=u; u++; } fru(i,k) fru(j,2) scanf("%d",&REA[i][j]); fru(i,k) fru(j,2) REA[i][j]--; fru(i,k) fru(j,2) ZAP[REA[i][j]].push_back(pii(REA[i][j^1],i)); fru(i,u) REP[i]=-1; for(int i=u-1;i>=0;--i) if(REP[i]==-1) dfs(i); printf("%lld\n",ret*2); 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 | #include <cstdio> #include <algorithm> #include <vector> #include <cassert> #define fru(j,n) for(int j=0;j<n;++j) #define tr(it,x) for(typeof(x.begin()) it=x.begin();it!=x.end();++it) #define x first #define y second using namespace std; typedef long long LL; typedef pair<int,int> pii; const int MAXN= 500005; int ILE[MAXN]; int D[MAXN][2]; int REP[MAXN],KTO[MAXN]; int REA[MAXN][2]; int find(int x) { return REP[x]==x?x:(REP[x]=find(REP[x])); } vector<pii> ZAP[MAXN]; vector<int> ROB[MAXN]; LL ret;int n; void dfs(int u) { // printf("dfs(%d)\n",u); tr(it,ZAP[u]) if(REP[it->x]!=-1) ROB[find(it->x)].push_back(it->y); REP[u]=u; if(u>=n) fru(j,2) { int u2=D[u][j]; // printf("z %d ide do %d\n",u,u2); dfs(u2); REP[u2]=u; assert(find(u)==u); } sort(ROB[u].begin(),ROB[u].end()); tr(it,ROB[u]) { int a=REA[*it][0],b=REA[*it][1], e=min(ILE[a],ILE[b]); ILE[a]-=e; ILE[b]-=e; ret+=e; } } int main() { int m,k; scanf("%d%d%d",&n,&m,&k); fru(i,n) scanf("%d",&ILE[i]); int u=n; fru(i,n) KTO[i]=i; fru(i,m) { int a,b; scanf("%d%d",&a,&b); --a;--b; D[u][0]=KTO[a]; D[u][1]=KTO[b]; KTO[b]=u; u++; } fru(i,k) fru(j,2) scanf("%d",&REA[i][j]); fru(i,k) fru(j,2) REA[i][j]--; fru(i,k) fru(j,2) ZAP[REA[i][j]].push_back(pii(REA[i][j^1],i)); fru(i,u) REP[i]=-1; for(int i=u-1;i>=0;--i) if(REP[i]==-1) dfs(i); printf("%lld\n",ret*2); return 0; } |