#include <cstdio> #include <vector> #include <algorithm> using namespace std; vector <int> UF; int ufind (int a) { if (UF[a] == a) return a; UF[a] = ufind(UF[a]); return UF[a]; } bool Union (int a, int b) { int ufa = ufind(a), ufb = ufind(b); if (ufa == ufb) return false; UF[a] = ufb; } int main () { int n, m, k; scanf ("%d %d %d", &n, &m, &k); UF.resize(n+1, 0); for (int i=1; i<=n; i++) UF[i] = i; int mass [n+1]; for (int i=1; i<=n; i++) { int x; scanf ("%d" ,&x); mass[i] = x; } for (int i=0; i<m; i++) { int a, b; scanf ("%d %d", &a, &b); Union (a, b); } long long answer = 0; for (int i=0; i<k; i++) { int a, b; scanf ("%d %d", &a, &b); int ufa = ufind(a), ufb = ufind(b); if (ufa == ufb) { int dmass = min (mass[a], mass[b]); mass[a] -= dmass; mass[b] -= dmass; answer = answer + dmass + dmass; } } printf ("%Ld\n", answer); 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 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; vector <int> UF; int ufind (int a) { if (UF[a] == a) return a; UF[a] = ufind(UF[a]); return UF[a]; } bool Union (int a, int b) { int ufa = ufind(a), ufb = ufind(b); if (ufa == ufb) return false; UF[a] = ufb; } int main () { int n, m, k; scanf ("%d %d %d", &n, &m, &k); UF.resize(n+1, 0); for (int i=1; i<=n; i++) UF[i] = i; int mass [n+1]; for (int i=1; i<=n; i++) { int x; scanf ("%d" ,&x); mass[i] = x; } for (int i=0; i<m; i++) { int a, b; scanf ("%d %d", &a, &b); Union (a, b); } long long answer = 0; for (int i=0; i<k; i++) { int a, b; scanf ("%d %d", &a, &b); int ufa = ufind(a), ufb = ufind(b); if (ufa == ufb) { int dmass = min (mass[a], mass[b]); mass[a] -= dmass; mass[b] -= dmass; answer = answer + dmass + dmass; } } printf ("%Ld\n", answer); return 0; } |