#include <stdio.h> #include <algorithm> #include <vector> using namespace std; using namespace std; typedef long long LL; const int MaxN = 200005, MaxV = 400005, LogMaxV = 22, MaxM = 200005, MaxK = 500005, ErrNoLCA = -31337; int N, M, K, V; int mass[MaxN]; // masy int destination[LogMaxV][MaxV]; // [0][v] - rodzic; potem 2^k to [k][v] int depth[MaxV]; // glebokosc wierzcholka w drzewie int actualLoc[MaxN]; // aktualne polozenie i-tej probowki vector<int> children[MaxV]; // dzieci (<= 2) wierzcholka int reactC[MaxK], reactD[MaxK]; vector<int> meeting[MaxV]; // numery par reagujacych substancji w momencie void input(){ scanf("%d%d%d", &N, &M, &K); V = N; for(int i = 0; i < N; i++){ scanf("%d", &mass[i]); actualLoc[i] = i; } for(int i = 0; i <= 2*N; i++) destination[0][i] = i; for(int i = 0; i < M; i++){ int a, b; scanf("%d%d", &a, &b); a--; b--; // tworzymy przejscie do nowej probowki z probowek zaw. 'a' i 'b' destination[0][actualLoc[a]] = V; destination[0][actualLoc[b]] = V; //destination[0][V] = V; children[V].push_back(actualLoc[a]); children[V].push_back(actualLoc[b]); //printf("%d => %d %d\n", V+1, actualLoc[a]+1, actualLoc[b]+1); actualLoc[b] = (V++); } for(int i = 0; i < K; i++){ scanf("%d%d", &reactC[i], &reactD[i]); reactC[i]--; reactD[i]--; } } /** OBSLUGA LCA **/ int logv; void process_lca(){ logv = 0; int pwr = 1; while(pwr < V){ pwr *= 2; logv++; } // przesuwamy for(int i = 1; i <= logv; i++){ //printf("%d :", i); for(int w = 0; w < V; w++){ destination[i][w] = destination[i-1][destination[i-1][w]]; //printf(" %d", destination[i][w]+1); } //printf("\n"); } } bool vis[MaxV]; int dfs(int v){ if(vis[v]) return depth[v]; vis[v] = true; if(destination[0][v] == v) return (depth[v] = 0); return (depth[v] = dfs(destination[0][v]) + 1); } void process_depth(){ fill(vis, vis+V, false); for(int i = 0; i < V; i++){ if(!vis[i]){ dfs(i); } } } int push(int v, int num){ // przesuwa 'v' o 'num' do gory for(int i = 0; i <= logv; i++){ if(num & (1<<i)) v = destination[i][v]; } return v; } int get_lca(int u, int v){ // brak LCA if(destination[logv][u] != destination[logv][v]) return ErrNoLCA; //printf("get_lca(%d, %d)", u+1, v+1); int depthU = depth[u], depthV = depth[v]; if(depthU < depthV) v = push(v, depthV-depthU); else u = push(u, depthU-depthV); //printf(" = get_lca(%d,%d)\n", u+1, v+1); if(u == v) return u; for(int i = logv; i >= 0; i--){ int newV = destination[i][v], newU = destination[i][u]; if(newV != newU){ v = newV; u = newU; } } return destination[0][v]; } /** KONIEC OBSLUGI LCA **/ LL result = 0; void dfs_pour(int w){ for(int s: children[w]){ dfs_pour(s); } for(int numReact: meeting[w]){ int c = reactC[numReact], d = reactD[numReact]; int decr = min(mass[c], mass[d]); result += 2*decr; mass[c] -= decr; mass[d] -= decr; //printf("Connecting %d with %d, decrease = %d\n", c+1, d+1, decr); } } int main(){ input(); process_lca(); process_depth(); // przetwarzamy reagenty for(int i = 0; i < K; i++){ int res = get_lca(reactC[i], reactD[i]); //printf("%d %d => %d\n", reactC[i]+1, reactD[i]+1, res+1); if(res != ErrNoLCA){ // dodajemy mozliwa reakcje do wierzcholka meeting[res].push_back(i); } } // DFS-ujemy fill(vis, vis+V, false); for(int i = 0; i < V; i++){ if(!vis[i] && destination[0][i] == i){ // nieodwiedzony korzen dfs_pour(i); } } printf("%lld\n", result); }
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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | #include <stdio.h> #include <algorithm> #include <vector> using namespace std; using namespace std; typedef long long LL; const int MaxN = 200005, MaxV = 400005, LogMaxV = 22, MaxM = 200005, MaxK = 500005, ErrNoLCA = -31337; int N, M, K, V; int mass[MaxN]; // masy int destination[LogMaxV][MaxV]; // [0][v] - rodzic; potem 2^k to [k][v] int depth[MaxV]; // glebokosc wierzcholka w drzewie int actualLoc[MaxN]; // aktualne polozenie i-tej probowki vector<int> children[MaxV]; // dzieci (<= 2) wierzcholka int reactC[MaxK], reactD[MaxK]; vector<int> meeting[MaxV]; // numery par reagujacych substancji w momencie void input(){ scanf("%d%d%d", &N, &M, &K); V = N; for(int i = 0; i < N; i++){ scanf("%d", &mass[i]); actualLoc[i] = i; } for(int i = 0; i <= 2*N; i++) destination[0][i] = i; for(int i = 0; i < M; i++){ int a, b; scanf("%d%d", &a, &b); a--; b--; // tworzymy przejscie do nowej probowki z probowek zaw. 'a' i 'b' destination[0][actualLoc[a]] = V; destination[0][actualLoc[b]] = V; //destination[0][V] = V; children[V].push_back(actualLoc[a]); children[V].push_back(actualLoc[b]); //printf("%d => %d %d\n", V+1, actualLoc[a]+1, actualLoc[b]+1); actualLoc[b] = (V++); } for(int i = 0; i < K; i++){ scanf("%d%d", &reactC[i], &reactD[i]); reactC[i]--; reactD[i]--; } } /** OBSLUGA LCA **/ int logv; void process_lca(){ logv = 0; int pwr = 1; while(pwr < V){ pwr *= 2; logv++; } // przesuwamy for(int i = 1; i <= logv; i++){ //printf("%d :", i); for(int w = 0; w < V; w++){ destination[i][w] = destination[i-1][destination[i-1][w]]; //printf(" %d", destination[i][w]+1); } //printf("\n"); } } bool vis[MaxV]; int dfs(int v){ if(vis[v]) return depth[v]; vis[v] = true; if(destination[0][v] == v) return (depth[v] = 0); return (depth[v] = dfs(destination[0][v]) + 1); } void process_depth(){ fill(vis, vis+V, false); for(int i = 0; i < V; i++){ if(!vis[i]){ dfs(i); } } } int push(int v, int num){ // przesuwa 'v' o 'num' do gory for(int i = 0; i <= logv; i++){ if(num & (1<<i)) v = destination[i][v]; } return v; } int get_lca(int u, int v){ // brak LCA if(destination[logv][u] != destination[logv][v]) return ErrNoLCA; //printf("get_lca(%d, %d)", u+1, v+1); int depthU = depth[u], depthV = depth[v]; if(depthU < depthV) v = push(v, depthV-depthU); else u = push(u, depthU-depthV); //printf(" = get_lca(%d,%d)\n", u+1, v+1); if(u == v) return u; for(int i = logv; i >= 0; i--){ int newV = destination[i][v], newU = destination[i][u]; if(newV != newU){ v = newV; u = newU; } } return destination[0][v]; } /** KONIEC OBSLUGI LCA **/ LL result = 0; void dfs_pour(int w){ for(int s: children[w]){ dfs_pour(s); } for(int numReact: meeting[w]){ int c = reactC[numReact], d = reactD[numReact]; int decr = min(mass[c], mass[d]); result += 2*decr; mass[c] -= decr; mass[d] -= decr; //printf("Connecting %d with %d, decrease = %d\n", c+1, d+1, decr); } } int main(){ input(); process_lca(); process_depth(); // przetwarzamy reagenty for(int i = 0; i < K; i++){ int res = get_lca(reactC[i], reactD[i]); //printf("%d %d => %d\n", reactC[i]+1, reactD[i]+1, res+1); if(res != ErrNoLCA){ // dodajemy mozliwa reakcje do wierzcholka meeting[res].push_back(i); } } // DFS-ujemy fill(vis, vis+V, false); for(int i = 0; i < V; i++){ if(!vis[i] && destination[0][i] == i){ // nieodwiedzony korzen dfs_pour(i); } } printf("%lld\n", result); } |