#include<cstdio> #include<vector> #include<set> #include<algorithm> using namespace std; struct dwa { long long a,b; dwa() {} dwa(long long x, long long y) {a = x, b = y;} }; struct comp { bool operator() (const dwa &A, const dwa &B) const { return A.a < B.a; } }; struct trzy { int a,b,c; trzy() {} trzy(int x, int y, int z) {a = x, b = y, c = z;} trzy(dwa X) {a = X.a, b = X.a, c = 0;} }; bool comp3(const trzy &A, const trzy &B) { if (A.a < B.a) return true; if (A.a > B.a) return false; if (A.b < B.b) return true; return false; } int G[200003]; dwa Q[200003]; vector<int> v[200003]; multiset<dwa,comp> S[200003]; dwa w[500003]; trzy R[1000005]; int n,m,k; bool comp2(const dwa &A, const dwa &B) { lower_bound(R+1,R+2*k+1,trzy(A),comp3)->c < lower_bound(R+1,R+2*k+1,trzy(B),comp3)->c; } void quicksort(dwa tab[],int lewy,int prawy) { if(lewy<prawy) { int m=lewy; for(int i=lewy+1;i<=prawy;i++) if(comp2(tab[i],tab[lewy])) swap(tab[++m],tab[i]); swap(tab[lewy],tab[m]); quicksort(tab,lewy,m-1); quicksort(tab,m+1,prawy); } } int main() { scanf("%d%d%d",&n,&m,&k); for (int i = 1; i <= n; i++) { scanf("%d",&G[i]); S[i].insert(dwa(i,G[i])); } for (int i = 0; i < m; i++) scanf("%d%d",&Q[i].a,&Q[i].b); for (int i = 1; i <= k; i++) { int a,b; scanf("%d%d",&a,&b); v[a].push_back(b); v[b].push_back(a); R[2*i] = trzy(a,b,i); R[2*i+1] = trzy(b,a,i); //int x = ((1ll*max(a,b)*q)%mod+min(a,b))%mod; //if (T[x] != 0) puts("T"); //else T[x] = i; //printf("%d\n",i); } sort(R+1,R+2*k+1,comp3); long long ans = 0ll; multiset<dwa>::iterator it = S[0].begin(); multiset<dwa>::iterator it2 = S[0].begin(); for (int i = 0; i < m; i++) { //printf("%lld\n",ans); //puts("F"); //printf("%d\n",i); //printf("%d %d\n",S[a].size(),S[b].size()); int ind = 0; int a = Q[i].a, b = Q[i].b; for (it = S[a].begin(); it != S[a].end(); it++) { int x = it->a; for (int j = 0; j < v[x].size(); j++) { it2 = S[b].find(dwa(v[x][j],0)); if (it2 != S[b].end()) w[ind++] = dwa(x,v[x][j]);//w.push_back(dwa(x,v[x][j])); } } //puts("G"); //puts("I"); //if (w.size() > 0) printf("%d\n",w.size()); /* for (int j = 0; j < w.size(); j++) { printf("%d ",lower_bound(R+1,R+2*k+1,trzy(w[j]),comp3)->c); } puts(""); */ quicksort(w,0,ind-1); //puts("H"); //puts("J"); for (int j = 0; j < ind; j++) { it = S[a].find(dwa(w[j].a,0)); it2 = S[b].find(dwa(w[j].b,0)); //puts("A"); if (it == S[a].end()) continue; if (it2 == S[b].end()) { S[b].insert(dwa(it->a,it->b)); S[a].erase(it); continue; } int a1 = it->a, b1 = it->b, a2 = it2->a, b2 = it2->b; //puts("B"); S[a].erase(it); S[b].erase(it2); //puts("C"); if (b1 > b2) { S[b].insert(dwa(a1,b1-b2)); ans += 2ll*(b2); //printf("%lld\n",2ll*(it2->b)); } else if (b1 < b2) { S[b].insert(dwa(a2,b2-b1)); ans += 2ll*(b1); //printf("%lld\n",2ll*(it->b)); } else ans += 2ll*(b1);//, printf("%lld\n",2ll*(it->b)); //puts("D"); } for (it = S[a].begin(); it != S[a].end(); it++) { S[b].insert(dwa(it->a,it->b)); } //puts("E"); S[a].clear(); //printf("%d %d\n",S[a].size(),S[b].size()); for (int j = 0; j <= ind; j++) w[j] = dwa(0,0); } printf("%lld\n",ans); 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | #include<cstdio> #include<vector> #include<set> #include<algorithm> using namespace std; struct dwa { long long a,b; dwa() {} dwa(long long x, long long y) {a = x, b = y;} }; struct comp { bool operator() (const dwa &A, const dwa &B) const { return A.a < B.a; } }; struct trzy { int a,b,c; trzy() {} trzy(int x, int y, int z) {a = x, b = y, c = z;} trzy(dwa X) {a = X.a, b = X.a, c = 0;} }; bool comp3(const trzy &A, const trzy &B) { if (A.a < B.a) return true; if (A.a > B.a) return false; if (A.b < B.b) return true; return false; } int G[200003]; dwa Q[200003]; vector<int> v[200003]; multiset<dwa,comp> S[200003]; dwa w[500003]; trzy R[1000005]; int n,m,k; bool comp2(const dwa &A, const dwa &B) { lower_bound(R+1,R+2*k+1,trzy(A),comp3)->c < lower_bound(R+1,R+2*k+1,trzy(B),comp3)->c; } void quicksort(dwa tab[],int lewy,int prawy) { if(lewy<prawy) { int m=lewy; for(int i=lewy+1;i<=prawy;i++) if(comp2(tab[i],tab[lewy])) swap(tab[++m],tab[i]); swap(tab[lewy],tab[m]); quicksort(tab,lewy,m-1); quicksort(tab,m+1,prawy); } } int main() { scanf("%d%d%d",&n,&m,&k); for (int i = 1; i <= n; i++) { scanf("%d",&G[i]); S[i].insert(dwa(i,G[i])); } for (int i = 0; i < m; i++) scanf("%d%d",&Q[i].a,&Q[i].b); for (int i = 1; i <= k; i++) { int a,b; scanf("%d%d",&a,&b); v[a].push_back(b); v[b].push_back(a); R[2*i] = trzy(a,b,i); R[2*i+1] = trzy(b,a,i); //int x = ((1ll*max(a,b)*q)%mod+min(a,b))%mod; //if (T[x] != 0) puts("T"); //else T[x] = i; //printf("%d\n",i); } sort(R+1,R+2*k+1,comp3); long long ans = 0ll; multiset<dwa>::iterator it = S[0].begin(); multiset<dwa>::iterator it2 = S[0].begin(); for (int i = 0; i < m; i++) { //printf("%lld\n",ans); //puts("F"); //printf("%d\n",i); //printf("%d %d\n",S[a].size(),S[b].size()); int ind = 0; int a = Q[i].a, b = Q[i].b; for (it = S[a].begin(); it != S[a].end(); it++) { int x = it->a; for (int j = 0; j < v[x].size(); j++) { it2 = S[b].find(dwa(v[x][j],0)); if (it2 != S[b].end()) w[ind++] = dwa(x,v[x][j]);//w.push_back(dwa(x,v[x][j])); } } //puts("G"); //puts("I"); //if (w.size() > 0) printf("%d\n",w.size()); /* for (int j = 0; j < w.size(); j++) { printf("%d ",lower_bound(R+1,R+2*k+1,trzy(w[j]),comp3)->c); } puts(""); */ quicksort(w,0,ind-1); //puts("H"); //puts("J"); for (int j = 0; j < ind; j++) { it = S[a].find(dwa(w[j].a,0)); it2 = S[b].find(dwa(w[j].b,0)); //puts("A"); if (it == S[a].end()) continue; if (it2 == S[b].end()) { S[b].insert(dwa(it->a,it->b)); S[a].erase(it); continue; } int a1 = it->a, b1 = it->b, a2 = it2->a, b2 = it2->b; //puts("B"); S[a].erase(it); S[b].erase(it2); //puts("C"); if (b1 > b2) { S[b].insert(dwa(a1,b1-b2)); ans += 2ll*(b2); //printf("%lld\n",2ll*(it2->b)); } else if (b1 < b2) { S[b].insert(dwa(a2,b2-b1)); ans += 2ll*(b1); //printf("%lld\n",2ll*(it->b)); } else ans += 2ll*(b1);//, printf("%lld\n",2ll*(it->b)); //puts("D"); } for (it = S[a].begin(); it != S[a].end(); it++) { S[b].insert(dwa(it->a,it->b)); } //puts("E"); S[a].clear(); //printf("%d %d\n",S[a].size(),S[b].size()); for (int j = 0; j <= ind; j++) w[j] = dwa(0,0); } printf("%lld\n",ans); return 0; } |