#include <cstdio> #include <algorithm> #include <vector> #define PB push_back using namespace std; int n, m, k; int tab[200001]; vector<int> v[200000]; struct q { int x, y; } tr[200000], rea[500000]; long long osad; bool czyW(int co, int gdzie) { auto low = lower_bound (v[gdzie].begin(), v[gdzie].end(), co); auto up = upper_bound (v[gdzie].begin(), v[gdzie].end(), co); return (low != up); } void sprawdz(int k) { for(int i = 0; i < k; ++i) { if(czyW(rea[i].x,k) && czyW(rea[i].y,k)) { int t = min(tab[rea[i].x],tab[rea[i].y]); osad += 2*t; tab[rea[i].x] -= t; tab[rea[i].y] -= t; } } } int main () { scanf("%d%d%d",&n,&m,&k); for(int i = 1; i <= n; ++i) { scanf("%d",&tab[i]); v[i].PB(i); } for(int i = 0; i < m; ++i) { scanf("%d%d",&tr[i].x,&tr[i].y); } for(int i = 0; i < k; ++i) { scanf("%d%d", &rea[i].x, &rea[i].y); if(rea[i].x > rea[i].y) swap(rea[i].x,rea[i].y); } for(int i = 0; i < m; ++i) { while(!v[tr[i].x].empty()) { v[tr[i].y].PB(v[tr[i].x].back()); v[tr[i].x].pop_back(); } sort(v[tr[i].y].begin(),v[tr[i].y].end()); sprawdz(tr[i].y); } printf("%lld\n", 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 | #include <cstdio> #include <algorithm> #include <vector> #define PB push_back using namespace std; int n, m, k; int tab[200001]; vector<int> v[200000]; struct q { int x, y; } tr[200000], rea[500000]; long long osad; bool czyW(int co, int gdzie) { auto low = lower_bound (v[gdzie].begin(), v[gdzie].end(), co); auto up = upper_bound (v[gdzie].begin(), v[gdzie].end(), co); return (low != up); } void sprawdz(int k) { for(int i = 0; i < k; ++i) { if(czyW(rea[i].x,k) && czyW(rea[i].y,k)) { int t = min(tab[rea[i].x],tab[rea[i].y]); osad += 2*t; tab[rea[i].x] -= t; tab[rea[i].y] -= t; } } } int main () { scanf("%d%d%d",&n,&m,&k); for(int i = 1; i <= n; ++i) { scanf("%d",&tab[i]); v[i].PB(i); } for(int i = 0; i < m; ++i) { scanf("%d%d",&tr[i].x,&tr[i].y); } for(int i = 0; i < k; ++i) { scanf("%d%d", &rea[i].x, &rea[i].y); if(rea[i].x > rea[i].y) swap(rea[i].x,rea[i].y); } for(int i = 0; i < m; ++i) { while(!v[tr[i].x].empty()) { v[tr[i].y].PB(v[tr[i].x].back()); v[tr[i].x].pop_back(); } sort(v[tr[i].y].begin(),v[tr[i].y].end()); sprawdz(tr[i].y); } printf("%lld\n", osad); return 0; } |