#include <iostream>
using namespace std;
int n, m, k;
int fiolka[200005];
pair<int,int> kolejnosc[200005];
pair<int,int> reakcja[500005];
int p[200005];
int w[200005];
long long int osad = 0;
int odjac;
int Find(int x)
{
return (p[x] < 0) ? x : p[x] = Find(p[x]);
}
void Union(int x, int y)
{
if((x = Find(x)) == (y = Find(y))) return;
if(w[x] > w[y]) p[y] = x;
else p[x] = y;
if(w[x] == w[y]) w[y]++;
}
int main()
{
ios_base::sync_with_stdio(0);
cin >> n >> m >> k;
for(int i = 1; i <= n; i++)
{
cin >> fiolka[i];
}
for(int i = 0; i < m; i++)
{
cin >> kolejnosc[i].first >> kolejnosc[i].second;
}
for(int i = 0; i < k; i++)
{
cin >> reakcja[i].first >> reakcja[i].second;
}
for(int i = 0; i <= n; i++)
{
p[i] = -1;
w[i] = -1;
}
for(int i = 0; i < m; i++)
{
Union(kolejnosc[i].first, kolejnosc[i].second);
for(int j = 0; j < k; j++)
{
if(Find(reakcja[j].first) == Find(reakcja[j].second))
{
odjac = min(fiolka[reakcja[j].first], fiolka[reakcja[j].second]);
fiolka[reakcja[j].first] -= odjac;
fiolka[reakcja[j].second] -= odjac;
osad += (2*odjac);
}
}
}
cout << osad;
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 | #include <iostream> using namespace std; int n, m, k; int fiolka[200005]; pair<int,int> kolejnosc[200005]; pair<int,int> reakcja[500005]; int p[200005]; int w[200005]; long long int osad = 0; int odjac; int Find(int x) { return (p[x] < 0) ? x : p[x] = Find(p[x]); } void Union(int x, int y) { if((x = Find(x)) == (y = Find(y))) return; if(w[x] > w[y]) p[y] = x; else p[x] = y; if(w[x] == w[y]) w[y]++; } int main() { ios_base::sync_with_stdio(0); cin >> n >> m >> k; for(int i = 1; i <= n; i++) { cin >> fiolka[i]; } for(int i = 0; i < m; i++) { cin >> kolejnosc[i].first >> kolejnosc[i].second; } for(int i = 0; i < k; i++) { cin >> reakcja[i].first >> reakcja[i].second; } for(int i = 0; i <= n; i++) { p[i] = -1; w[i] = -1; } for(int i = 0; i < m; i++) { Union(kolejnosc[i].first, kolejnosc[i].second); for(int j = 0; j < k; j++) { if(Find(reakcja[j].first) == Find(reakcja[j].second)) { odjac = min(fiolka[reakcja[j].first], fiolka[reakcja[j].second]); fiolka[reakcja[j].first] -= odjac; fiolka[reakcja[j].second] -= odjac; osad += (2*odjac); } } } cout << osad; return 0; } |
English