#include "cstdio" #include "algorithm" #define gc getchar_unlocked void scanint(long long &x) { register long long c = gc(); x = 0; unsigned long long neg = 0; for(;((c<48 || c>57) && c != '-');c = gc()); if(c=='-') {neg=1;c=gc();} for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;} if(neg) x=-x; } long long w,n,x,m,k,a,b,tab[200005],ile[200005],sub[200005]; std::pair <long long,long long> lacze[200005],tabk[500005]; long long Find (long long a) { if (tab[a]==a) return a; long long fa = Find(tab[a]); tab[a] = fa; return fa; } bool Union(long long a, long long b) { long long fa = Find(a); long long fb = Find(b); if (fa==fb) return false; if (ile[fa] <= ile[fb]) { ile[fb] += ile[fa]; tab[fa] = fb; } else { ile[fa] += ile[fb]; tab[fb] = fa; } return true; } int main() { scanint (n); scanint (m); scanint (k); for (int i=0; i<n; i++) { scanint (sub[i]); tab[i]=i; ile[i]=1; } if (m*k<1000000000) { for (int i=0; i<m; i++) scanint (lacze[i].first), scanint (lacze[i].second); for (int i=0; i<k; i++) scanint (tabk[i].first), scanint (tabk[i].second); for (int i=0; i<m; i++) { Union (lacze[i].first,lacze[i].second); for (int j=0; j<k; j++) { if (Find(tabk[j].first)==Find(tabk[j].second)) { x=std::min(sub[tabk[j].first-1],sub[tabk[j].second-1]); w+=(x*2); sub[tabk[j].first-1]-=x; sub[tabk[j].second-1]-=x; } } } printf ("%lld\n", w); } else { for (int i=0; i<m; i++) { scanint (a); scanint (b); Union (a,b); } for (int i=0; i<k; i++) { scanint (a); scanint (b); if (Find(a)==Find(b)) { x=std::min(sub[a-1],sub[b-1]); w+=(x*2); sub[a-1]-=x; sub[b-1]-=x; } } printf ("%lld\n", w); } 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 | #include "cstdio" #include "algorithm" #define gc getchar_unlocked void scanint(long long &x) { register long long c = gc(); x = 0; unsigned long long neg = 0; for(;((c<48 || c>57) && c != '-');c = gc()); if(c=='-') {neg=1;c=gc();} for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;} if(neg) x=-x; } long long w,n,x,m,k,a,b,tab[200005],ile[200005],sub[200005]; std::pair <long long,long long> lacze[200005],tabk[500005]; long long Find (long long a) { if (tab[a]==a) return a; long long fa = Find(tab[a]); tab[a] = fa; return fa; } bool Union(long long a, long long b) { long long fa = Find(a); long long fb = Find(b); if (fa==fb) return false; if (ile[fa] <= ile[fb]) { ile[fb] += ile[fa]; tab[fa] = fb; } else { ile[fa] += ile[fb]; tab[fb] = fa; } return true; } int main() { scanint (n); scanint (m); scanint (k); for (int i=0; i<n; i++) { scanint (sub[i]); tab[i]=i; ile[i]=1; } if (m*k<1000000000) { for (int i=0; i<m; i++) scanint (lacze[i].first), scanint (lacze[i].second); for (int i=0; i<k; i++) scanint (tabk[i].first), scanint (tabk[i].second); for (int i=0; i<m; i++) { Union (lacze[i].first,lacze[i].second); for (int j=0; j<k; j++) { if (Find(tabk[j].first)==Find(tabk[j].second)) { x=std::min(sub[tabk[j].first-1],sub[tabk[j].second-1]); w+=(x*2); sub[tabk[j].first-1]-=x; sub[tabk[j].second-1]-=x; } } } printf ("%lld\n", w); } else { for (int i=0; i<m; i++) { scanint (a); scanint (b); Union (a,b); } for (int i=0; i<k; i++) { scanint (a); scanint (b); if (Find(a)==Find(b)) { x=std::min(sub[a-1],sub[b-1]); w+=(x*2); sub[a-1]-=x; sub[b-1]-=x; } } printf ("%lld\n", w); } return 0; } |