#include <cstdio> #include <algorithm> #include <vector> #define st first #define nd second const int MAXN = 200005, MAXK = 500005; int n,m,k,realK,firstRoot,maxTreeHeight,maxSteps,degout[MAXN],nextRoot[MAXN], used[MAXN],treeNo[MAXN],level[MAXN],p[20][MAXN],activeOrder[MAXK], costToParent[MAXN],connectionStep[MAXK]; long long weight[MAXN]; std::pair<int,int> active[MAXK]; std::vector< std::pair<int,int> > graph[MAXN]; void init(); void dfs(int,int,int); bool comp(int,int); void calcConnSteps(); int main() { scanf("%d%d%d", &n, &m, &k); for(int i = 0; i < n; ++i) scanf("%lld", &weight[i]); for(int i = 0; i < m; ++i) { int a,b; scanf("%d%d", &a, &b); a--; b--; degout[a]++; //graph[a].push_back(std::make_pair(b,i)); // chyba niepotrzebne graph[b].push_back(std::make_pair(a,i)); } //printf("graph created\n"); init(); //printf("initialization complete\n"); realK = 0; for(int i = 0; i < k; ++i) { int a,b; scanf("%d%d", &a, &b); a--; b--; if(treeNo[a] == treeNo[b]) active[realK++] = std::make_pair(a,b); } //printf("active filter complete\n"); calcConnSteps(); //printf("steps calculation complete\n"); /* printf("Active have steps:\n"); for(int i = 0; i < realK; ++i) printf("[%d,%d]: %d\n", active[i].st+1, active[i].nd+1, connectionStep[i]); */ std::sort(activeOrder,activeOrder+realK,comp); //printf("sort active things complete\n"); /* printf("sorted: "); for(int i = 0; i < realK; ++i) printf("(%d,%d) ", active[activeOrder[i]].st+1, active[activeOrder[i]].nd+1); printf("\n"); */ long long result = 0; for(int i = 0; i < realK; ++i) { long long reaction = std::min(weight[active[activeOrder[i]].st],weight[active[activeOrder[i]].nd]); weight[active[activeOrder[i]].st] -= reaction; weight[active[activeOrder[i]].nd] -= reaction; result += 2*reaction; } printf("%lld\n", result); } void init() { for(int i = 0; i < k; ++i) activeOrder[i] = i; firstRoot = -1; for(int i = n-1; i >= 0; --i) { nextRoot[i] = firstRoot; if(degout[i] == 0) firstRoot = i; } int currTree = firstRoot; while(currTree != -1) { //printf("tree in root %d\n", currTree); dfs(currTree,currTree,0); //printf("dfs part done\n"); p[0][currTree] = currTree; currTree = nextRoot[currTree]; } //printf("ok\n"); int pow2 = 1, iterations = 0; while(pow2 < maxTreeHeight) { pow2 *= 2; iterations++; } maxSteps = iterations; //printf("maxSteps: %d\n", maxSteps); for(int i = 1; i < maxSteps; ++i) for(int j = 0; j < n; ++j) p[i][j] = p[i-1][p[i-1][j]]; //printf("p array ready\n"); } void dfs(int v, int treeMark, int depth) { level[v] = depth; used[v] = true; maxTreeHeight = std::max(maxTreeHeight,depth); treeNo[v] = treeMark; for(int i = 0; i < graph[v].size(); ++i) { if(used[graph[v][i].st]) continue; p[0][graph[v][i].st] = v; costToParent[graph[v][i].st] = graph[v][i].nd; dfs(graph[v][i].st,treeMark,depth+1); } } int equalize(int v, int diff) { int node = v; for(int i = 0; i < maxSteps; ++i) if((1<<i) & diff) node = p[i][node]; return node; } int LCA(int a, int b, int& nodeA, int& nodeB) { nodeA = a, nodeB = b; int diff = std::abs(level[a]-level[b]); if(level[a] < level[b]) nodeB = equalize(nodeB,diff); else if(level[a] > level[b]) nodeA = equalize(nodeA,diff); if(nodeA == nodeB) return nodeA; for(int i = maxSteps; i >= 0; --i) { if(p[i][nodeA] != p[i][nodeB]) { nodeA = p[i][nodeA]; nodeB = p[i][nodeB]; } } if(p[0][nodeA] != p[0][nodeB]) printf("Something's wrong...\n"); return p[0][nodeA]; } bool isOnPath(int a, int b, int lca) { if(lca == a || lca == b) return true; return false; } void calcConnSteps() { int tmpNodeSt, tmpNodeNd, tmp; for(int i = 0; i < realK; ++i) { int lca = LCA(active[i].st,active[i].nd,tmpNodeSt,tmpNodeNd); //printf("found lca(%d,%d): %d\n", active[i].st+1, active[i].nd+1, lca+1); if(isOnPath(active[i].st,active[i].nd,lca)) { if(active[i].st == lca) tmp = equalize(active[i].nd, std::abs(level[active[i].st]-level[active[i].nd])-1); else tmp = equalize(active[i].st, std::abs(level[active[i].st]-level[active[i].nd])-1); connectionStep[i] = costToParent[tmp]; } else connectionStep[i] = std::max(costToParent[tmpNodeSt],costToParent[tmpNodeNd]); } } bool comp(int a, int b) { if(connectionStep[a] == connectionStep[b]) return a < b; return connectionStep[a] < connectionStep[b]; }
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 187 | #include <cstdio> #include <algorithm> #include <vector> #define st first #define nd second const int MAXN = 200005, MAXK = 500005; int n,m,k,realK,firstRoot,maxTreeHeight,maxSteps,degout[MAXN],nextRoot[MAXN], used[MAXN],treeNo[MAXN],level[MAXN],p[20][MAXN],activeOrder[MAXK], costToParent[MAXN],connectionStep[MAXK]; long long weight[MAXN]; std::pair<int,int> active[MAXK]; std::vector< std::pair<int,int> > graph[MAXN]; void init(); void dfs(int,int,int); bool comp(int,int); void calcConnSteps(); int main() { scanf("%d%d%d", &n, &m, &k); for(int i = 0; i < n; ++i) scanf("%lld", &weight[i]); for(int i = 0; i < m; ++i) { int a,b; scanf("%d%d", &a, &b); a--; b--; degout[a]++; //graph[a].push_back(std::make_pair(b,i)); // chyba niepotrzebne graph[b].push_back(std::make_pair(a,i)); } //printf("graph created\n"); init(); //printf("initialization complete\n"); realK = 0; for(int i = 0; i < k; ++i) { int a,b; scanf("%d%d", &a, &b); a--; b--; if(treeNo[a] == treeNo[b]) active[realK++] = std::make_pair(a,b); } //printf("active filter complete\n"); calcConnSteps(); //printf("steps calculation complete\n"); /* printf("Active have steps:\n"); for(int i = 0; i < realK; ++i) printf("[%d,%d]: %d\n", active[i].st+1, active[i].nd+1, connectionStep[i]); */ std::sort(activeOrder,activeOrder+realK,comp); //printf("sort active things complete\n"); /* printf("sorted: "); for(int i = 0; i < realK; ++i) printf("(%d,%d) ", active[activeOrder[i]].st+1, active[activeOrder[i]].nd+1); printf("\n"); */ long long result = 0; for(int i = 0; i < realK; ++i) { long long reaction = std::min(weight[active[activeOrder[i]].st],weight[active[activeOrder[i]].nd]); weight[active[activeOrder[i]].st] -= reaction; weight[active[activeOrder[i]].nd] -= reaction; result += 2*reaction; } printf("%lld\n", result); } void init() { for(int i = 0; i < k; ++i) activeOrder[i] = i; firstRoot = -1; for(int i = n-1; i >= 0; --i) { nextRoot[i] = firstRoot; if(degout[i] == 0) firstRoot = i; } int currTree = firstRoot; while(currTree != -1) { //printf("tree in root %d\n", currTree); dfs(currTree,currTree,0); //printf("dfs part done\n"); p[0][currTree] = currTree; currTree = nextRoot[currTree]; } //printf("ok\n"); int pow2 = 1, iterations = 0; while(pow2 < maxTreeHeight) { pow2 *= 2; iterations++; } maxSteps = iterations; //printf("maxSteps: %d\n", maxSteps); for(int i = 1; i < maxSteps; ++i) for(int j = 0; j < n; ++j) p[i][j] = p[i-1][p[i-1][j]]; //printf("p array ready\n"); } void dfs(int v, int treeMark, int depth) { level[v] = depth; used[v] = true; maxTreeHeight = std::max(maxTreeHeight,depth); treeNo[v] = treeMark; for(int i = 0; i < graph[v].size(); ++i) { if(used[graph[v][i].st]) continue; p[0][graph[v][i].st] = v; costToParent[graph[v][i].st] = graph[v][i].nd; dfs(graph[v][i].st,treeMark,depth+1); } } int equalize(int v, int diff) { int node = v; for(int i = 0; i < maxSteps; ++i) if((1<<i) & diff) node = p[i][node]; return node; } int LCA(int a, int b, int& nodeA, int& nodeB) { nodeA = a, nodeB = b; int diff = std::abs(level[a]-level[b]); if(level[a] < level[b]) nodeB = equalize(nodeB,diff); else if(level[a] > level[b]) nodeA = equalize(nodeA,diff); if(nodeA == nodeB) return nodeA; for(int i = maxSteps; i >= 0; --i) { if(p[i][nodeA] != p[i][nodeB]) { nodeA = p[i][nodeA]; nodeB = p[i][nodeB]; } } if(p[0][nodeA] != p[0][nodeB]) printf("Something's wrong...\n"); return p[0][nodeA]; } bool isOnPath(int a, int b, int lca) { if(lca == a || lca == b) return true; return false; } void calcConnSteps() { int tmpNodeSt, tmpNodeNd, tmp; for(int i = 0; i < realK; ++i) { int lca = LCA(active[i].st,active[i].nd,tmpNodeSt,tmpNodeNd); //printf("found lca(%d,%d): %d\n", active[i].st+1, active[i].nd+1, lca+1); if(isOnPath(active[i].st,active[i].nd,lca)) { if(active[i].st == lca) tmp = equalize(active[i].nd, std::abs(level[active[i].st]-level[active[i].nd])-1); else tmp = equalize(active[i].st, std::abs(level[active[i].st]-level[active[i].nd])-1); connectionStep[i] = costToParent[tmp]; } else connectionStep[i] = std::max(costToParent[tmpNodeSt],costToParent[tmpNodeNd]); } } bool comp(int a, int b) { if(connectionStep[a] == connectionStep[b]) return a < b; return connectionStep[a] < connectionStep[b]; } |