#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int FU[200005];
int fu( int a )
{if (FU[a]==a) return a; return FU[a]=fu(FU[a]); }
void un( int a, int b )
{
a=fu(a);
b=fu(b);
if ( a == b ) return;
FU[a]=b;
}
long long wyn;
int tt;
int G[200005];
int P[500005][2];
int P_odw[500005];
int IL[200005];
vector<int> KR[200005];
vector<int> PROC[200005];
vector<int> V[200005];
void DFS( int w )
{
for ( int i=0; i<(int)V[w].size(); i++ )
{
int v = V[w][i];
if ( P_odw[ v ] == tt )
{
int _s;
if ( w == P[ v ][0] )
_s = P[ v ][1];
else _s = P[ v ][0];
PROC[ fu(_s) ].push_back( v );
} else P_odw[ v ] = tt;
}
for ( int i=0; i<(int)KR[w].size(); i++ )
{
int w2 = KR[w][i];
DFS( w2 );
sort( PROC[w].begin(), PROC[w].end() );
for ( int j=0; j<(int)PROC[w].size(); j++ )
{
int p = PROC[w][j];
int a = P[p][0];
int b = P[p][1];
int c = min( G[a], G[b] );
wyn += 2*c;
G[a] -= c;
G[b] -= c;
}
PROC[w].clear();
un( w2, w );
}
}
int main()
{
int n,m,k;
scanf( "%d%d%d", &n, &m, &k );
for ( int i=0; i<n; i++ )
{
scanf( "%d", G+i );
FU[i]=i; IL[i]=0;
}
for ( int i=0; i<m; i++ )
{
int a,b;
scanf( "%d%d", &a, &b );
a--; b--;
KR[ b ].push_back(a);
IL[a]++;
}
for ( int i=0; i<k; i++ )
{
int a,b;
scanf( "%d%d", &a, &b );
a--; b--;
P[i][0]=a; P[i][1]=b;
V[a].push_back( i );
V[b].push_back( i );
P_odw[ i ] = -1;
}
wyn=0;
for ( int i=0; i<n; i++ ) if ( !IL[i] ) {tt=i+1; DFS( i );}
printf("%lld\n",wyn);
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 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; int FU[200005]; int fu( int a ) {if (FU[a]==a) return a; return FU[a]=fu(FU[a]); } void un( int a, int b ) { a=fu(a); b=fu(b); if ( a == b ) return; FU[a]=b; } long long wyn; int tt; int G[200005]; int P[500005][2]; int P_odw[500005]; int IL[200005]; vector<int> KR[200005]; vector<int> PROC[200005]; vector<int> V[200005]; void DFS( int w ) { for ( int i=0; i<(int)V[w].size(); i++ ) { int v = V[w][i]; if ( P_odw[ v ] == tt ) { int _s; if ( w == P[ v ][0] ) _s = P[ v ][1]; else _s = P[ v ][0]; PROC[ fu(_s) ].push_back( v ); } else P_odw[ v ] = tt; } for ( int i=0; i<(int)KR[w].size(); i++ ) { int w2 = KR[w][i]; DFS( w2 ); sort( PROC[w].begin(), PROC[w].end() ); for ( int j=0; j<(int)PROC[w].size(); j++ ) { int p = PROC[w][j]; int a = P[p][0]; int b = P[p][1]; int c = min( G[a], G[b] ); wyn += 2*c; G[a] -= c; G[b] -= c; } PROC[w].clear(); un( w2, w ); } } int main() { int n,m,k; scanf( "%d%d%d", &n, &m, &k ); for ( int i=0; i<n; i++ ) { scanf( "%d", G+i ); FU[i]=i; IL[i]=0; } for ( int i=0; i<m; i++ ) { int a,b; scanf( "%d%d", &a, &b ); a--; b--; KR[ b ].push_back(a); IL[a]++; } for ( int i=0; i<k; i++ ) { int a,b; scanf( "%d%d", &a, &b ); a--; b--; P[i][0]=a; P[i][1]=b; V[a].push_back( i ); V[b].push_back( i ); P_odw[ i ] = -1; } wyn=0; for ( int i=0; i<n; i++ ) if ( !IL[i] ) {tt=i+1; DFS( i );} printf("%lld\n",wyn); return 0; } |
English