/* Arek Wróbel - skater * * Zadanie: Fiolki * Konkurs: PA2014, runda 5B */ #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include <map> using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef vector<int> VI; #define REP(I, N) for(int I=0; I<(N); ++I) #define FOR(I, M, N) for(int I=(M); I<=(N); ++I) #define FORD(I, M, N) for(int I=(M); I>=(N); --I) #define FOREACH(IT, CON) for(__typeof(CON.begin()) IT=CON.begin(); IT!=CON.end(); ++IT) #define ST first #define ND second #define MP make_pair #define PB push_back const int INF=1000000000; const LL INFLL=1000000000000000000LL; //struct Subst { // map<int, LL> s; //}; typedef map<int, LL> Subst; int n, m, k; Subst subst[200009]; PII kroki[200009]; PII reakcje[500009]; LL osad = 0; void pP(pair<int, LL> p) { printf(" (%d: %lld)\n", p.ST, p.ND); } int main() { //wej scanf("%d%d%d", &n, &m, &k); REP(i, n) { LL g; scanf("%lld", &g); subst[i][i]=g; } REP(i, m) { int a, b; scanf("%d%d", &a, &b); kroki[i] = MP(a-1, b-1); } REP(i, k) { int c, d; scanf("%d%d", &c, &d); reakcje[i] = MP(c-1, d-1); } //prog REP(I, m){ int A = kroki[I].ST, B = kroki[I].ND; // printf("%d: %d %d\n", I, A, B); // printf(" prob %d\n", A); FOREACH(it, subst[A]) pP(*it); // printf(" prob %d\n", B); FOREACH(it, subst[B]) pP(*it); if (subst[A].size() > subst[B].size()) swap(subst[A], subst[B]); FOREACH(it, subst[A]) subst[B].insert(*it); subst[A].clear(); REP(i, k){ int a = reakcje[i].ST, b = reakcje[i].ND; Subst::iterator ia, ib; ia = subst[B].find(a); if (ia == subst[B].end()) continue; ib = subst[B].find(b); if (ib == subst[B].end()) continue; int x = min((*ia).ND, (*ib).ND); (*ia).ND -= x; (*ib).ND -= x; osad += 2*x; if ((*ia).ND == 0) subst[B].erase(a); if ((*ib).ND == 0) subst[B].erase(b); } } //wyj 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 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 | /* Arek Wróbel - skater * * Zadanie: Fiolki * Konkurs: PA2014, runda 5B */ #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include <map> using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef vector<int> VI; #define REP(I, N) for(int I=0; I<(N); ++I) #define FOR(I, M, N) for(int I=(M); I<=(N); ++I) #define FORD(I, M, N) for(int I=(M); I>=(N); --I) #define FOREACH(IT, CON) for(__typeof(CON.begin()) IT=CON.begin(); IT!=CON.end(); ++IT) #define ST first #define ND second #define MP make_pair #define PB push_back const int INF=1000000000; const LL INFLL=1000000000000000000LL; //struct Subst { // map<int, LL> s; //}; typedef map<int, LL> Subst; int n, m, k; Subst subst[200009]; PII kroki[200009]; PII reakcje[500009]; LL osad = 0; void pP(pair<int, LL> p) { printf(" (%d: %lld)\n", p.ST, p.ND); } int main() { //wej scanf("%d%d%d", &n, &m, &k); REP(i, n) { LL g; scanf("%lld", &g); subst[i][i]=g; } REP(i, m) { int a, b; scanf("%d%d", &a, &b); kroki[i] = MP(a-1, b-1); } REP(i, k) { int c, d; scanf("%d%d", &c, &d); reakcje[i] = MP(c-1, d-1); } //prog REP(I, m){ int A = kroki[I].ST, B = kroki[I].ND; // printf("%d: %d %d\n", I, A, B); // printf(" prob %d\n", A); FOREACH(it, subst[A]) pP(*it); // printf(" prob %d\n", B); FOREACH(it, subst[B]) pP(*it); if (subst[A].size() > subst[B].size()) swap(subst[A], subst[B]); FOREACH(it, subst[A]) subst[B].insert(*it); subst[A].clear(); REP(i, k){ int a = reakcje[i].ST, b = reakcje[i].ND; Subst::iterator ia, ib; ia = subst[B].find(a); if (ia == subst[B].end()) continue; ib = subst[B].find(b); if (ib == subst[B].end()) continue; int x = min((*ia).ND, (*ib).ND); (*ia).ND -= x; (*ib).ND -= x; osad += 2*x; if ((*ia).ND == 0) subst[B].erase(a); if ((*ib).ND == 0) subst[B].erase(b); } } //wyj printf("%lld\n", osad); return 0; } |