#include <set> #include <stdio.h> #include <unistd.h> #define K 500000 #define N 200000 using namespace std; static int getint() { static char buf[4096]; static int i,s; int val = 0, rs = 1; for(;;i++) { if(i==s) { s = read(0,buf,4096); if(s<=0) break; i = 0; } if(buf[i]>' ') { rs = 0; val = val * 10 + buf[i]-'0'; } else if(!rs) break; } return val; } set<int> xs[N], *xp[N]; int left[N]; int mov[N*2]; int re[K*2]; int main() { int n,m,i,k; long long res = 0; n = getint(); m = getint(); k = getint(); for (i = 0; i<n; i++) xp[i] = xs + i; for (i = 0; i<n; i++) left[i] = getint(); for (i = 0; i<2*m; i++) mov[i] = getint(); for (i = 0; i<2*k; i++) { re[i] = getint(); xs[re[i]-1].insert(i/2); } for (i = 0; i<2*m; i+=2) { int x1 = mov[i] - 1, x2 = mov[i+1] - 1; if (xp[x1]->size() > xp[x2]->size()) { set<int> *tmp = xp[x1]; xp[x1] = xp[x2]; xp[x2] = tmp; } for(set<int>::iterator it = xp[x1]->begin(); it != xp[x1]->end(); ++it) { set<int>::iterator jt = xp[x2]->find(*it); if (jt != xp[x2]->end()) { int a = re[*it*2]-1, b = re[*it*2+1]-1; int v = min(left[a], left[b]); left[a] -= v; left[b] -= v; res += 2ll * v; xp[x2]->erase(jt); } else { xp[x2]->insert(*it); } } xp[x1]->clear(); } printf("%lld\n", res); 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 <set> #include <stdio.h> #include <unistd.h> #define K 500000 #define N 200000 using namespace std; static int getint() { static char buf[4096]; static int i,s; int val = 0, rs = 1; for(;;i++) { if(i==s) { s = read(0,buf,4096); if(s<=0) break; i = 0; } if(buf[i]>' ') { rs = 0; val = val * 10 + buf[i]-'0'; } else if(!rs) break; } return val; } set<int> xs[N], *xp[N]; int left[N]; int mov[N*2]; int re[K*2]; int main() { int n,m,i,k; long long res = 0; n = getint(); m = getint(); k = getint(); for (i = 0; i<n; i++) xp[i] = xs + i; for (i = 0; i<n; i++) left[i] = getint(); for (i = 0; i<2*m; i++) mov[i] = getint(); for (i = 0; i<2*k; i++) { re[i] = getint(); xs[re[i]-1].insert(i/2); } for (i = 0; i<2*m; i+=2) { int x1 = mov[i] - 1, x2 = mov[i+1] - 1; if (xp[x1]->size() > xp[x2]->size()) { set<int> *tmp = xp[x1]; xp[x1] = xp[x2]; xp[x2] = tmp; } for(set<int>::iterator it = xp[x1]->begin(); it != xp[x1]->end(); ++it) { set<int>::iterator jt = xp[x2]->find(*it); if (jt != xp[x2]->end()) { int a = re[*it*2]-1, b = re[*it*2+1]-1; int v = min(left[a], left[b]); left[a] -= v; left[b] -= v; res += 2ll * v; xp[x2]->erase(jt); } else { xp[x2]->insert(*it); } } xp[x1]->clear(); } printf("%lld\n", res); return 0; } |