#include <stack> #include <vector> #include <set> #include <iostream> #include <climits> #include <stack> using namespace std; struct child { int indexOfChild; long long maxValue; int indexOfMaxValue; bool operator < (const child& ch) const { if(maxValue == ch.maxValue) return (indexOfMaxValue < ch.indexOfMaxValue); else return (maxValue > ch.maxValue); } }; struct node { int parent = -1; long long costToParent; long long value; int indexOfMaxValue = -1; long long maxValue = LONG_LONG_MIN; //Set - maxymalne wartosci w danym poddrzewie set<child> children; }; node tree[100001]; int n, q; int previousDayNode = -1; void buildTree(); void updateNode(int nIndex, long long newValue); void updatePath(int nIndex, long long newPathCost); void updateNewMax(int nodeIndex, int childIndex, int oldMaxIndex, int newMaxIndex, long long oldMaxValue, long long newMaxValue); //maxNode, maxValue pair<int, long long> searchForMaxDay(int nodeIndex); pair<int, long long> searchForBottomMaxDay(int nodeIndex); pair<int, long long> searchForUpperMaxDay(int nodeIndex); int main() { ios_base::sync_with_stdio(0); cin >> n >> q; buildTree(); for(int i = 0; i < q; i++){ int type; cin >> type; if(type == 1){ int node; long long newValue; cin >> node >> newValue; node--; updateNode(node, newValue); } else{ int nodeA, nodeB; long long newCost; cin >> nodeA >> nodeB >> newCost; if (tree[nodeA].parent == nodeB) updatePath(nodeA, newCost); else updatePath(nodeB, newCost); } pair<int, long long> newDay; if(previousDayNode == -1) newDay = searchForMaxDay(0); else newDay = searchForMaxDay(previousDayNode); previousDayNode = newDay.first; cout << newDay.first + 1 << endl; } return 0; } void buildTree(){ vector<int> values(n); vector<bool> wasntChecked(n); vector< vector< pair<int, int> > > connections (n); stack<int> checked; for (int i = 0; i < n; i++) { cin >> values[i]; wasntChecked[i] = true; } for(int i = 0; i < n - 1; i++){ int a, b; long long cost; cin >> a >> b >> cost; a--; b--; connections[a].push_back(make_pair(b, cost)); connections[b].push_back(make_pair(a, cost)); } tree[0].parent = 0; tree[0].costToParent = LONG_LONG_MAX; checked.push(0); while (!checked.empty()) { int index = checked.top(); checked.pop(); wasntChecked[index] = false; for (int i = 0; i < connections[index].size(); i++) { if(wasntChecked[connections[index][i].first]){ tree[connections[index][i].first].parent = index; tree[connections[index][i].first].costToParent = connections[index][i].second; checked.push(connections[index][i].first); } } } for (int i = 0; i < n; i++) updateNode(i, values[i]); } void updateNode(int nIndex, long long newValue){ tree[nIndex].value = newValue; long long oldMaxValue = tree[nIndex].maxValue; int oldMaxIndex = tree[nIndex].indexOfMaxValue; if(newValue > oldMaxValue){ tree[nIndex].maxValue = newValue; tree[nIndex].indexOfMaxValue = nIndex; //Przekazujemy do gory if(tree[nIndex].parent != nIndex) updateNewMax(tree[nIndex].parent, nIndex, oldMaxIndex, nIndex, oldMaxValue - tree[nIndex].costToParent, newValue - tree[nIndex].costToParent); } else if(newValue == oldMaxValue && nIndex < oldMaxIndex){ tree[nIndex].indexOfMaxValue = nIndex; if(tree[nIndex].parent != nIndex) updateNewMax(tree[nIndex].parent, nIndex, oldMaxIndex, nIndex, oldMaxValue - tree[nIndex].costToParent, newValue - tree[nIndex].costToParent); } else if(nIndex == oldMaxIndex){ child maxChild = *tree[nIndex].children.begin(); if(maxChild.maxValue > newValue){ tree[nIndex].indexOfMaxValue = maxChild.indexOfMaxValue; tree[nIndex].maxValue = maxChild.maxValue; if(tree[nIndex].parent != nIndex) updateNewMax(tree[nIndex].parent, nIndex, oldMaxIndex, maxChild.indexOfMaxValue, oldMaxValue - tree[nIndex].costToParent, maxChild.maxValue - tree[nIndex].costToParent); } else if(maxChild.maxValue == newValue && maxChild.indexOfMaxValue < nIndex){ tree[nIndex].indexOfMaxValue = maxChild.indexOfMaxValue; tree[nIndex].maxValue = maxChild.maxValue; if(tree[nIndex].parent != nIndex) updateNewMax(tree[nIndex].parent, nIndex, oldMaxIndex, maxChild.indexOfMaxValue, oldMaxValue - tree[nIndex].costToParent, maxChild.maxValue - tree[nIndex].costToParent); } else if(maxChild.maxValue < newValue){ tree[nIndex].maxValue = newValue; if(tree[nIndex].parent != nIndex) updateNewMax(tree[nIndex].parent, nIndex, oldMaxIndex, nIndex, oldMaxValue - tree[nIndex].costToParent, newValue - tree[nIndex].costToParent); } } } void updatePath(int nIndex, long long newPathCost){ long long oldPathCost = tree[nIndex].costToParent; tree[nIndex].costToParent = newPathCost; if(tree[nIndex].parent != nIndex) updateNewMax(tree[nIndex].parent, nIndex, tree[nIndex].indexOfMaxValue, tree[nIndex].indexOfMaxValue, tree[nIndex].maxValue - oldPathCost, tree[nIndex].maxValue - newPathCost); } void updateNewMax(int nodeIndex, int childIndex, int oldMaxIndex, int newMaxIndex, long long oldMaxValue, long long newMaxValue){ if(nodeIndex == -1 || childIndex == -1 || oldMaxIndex == -1 || newMaxIndex == -1) return; child oldChild, newChild; oldChild.indexOfChild = childIndex; oldChild.indexOfMaxValue = oldMaxIndex; oldChild.maxValue = oldMaxValue; newChild.indexOfChild = childIndex; newChild.indexOfMaxValue = newMaxIndex; newChild.maxValue = newMaxValue; tree[nodeIndex].children.erase(oldChild); tree[nodeIndex].children.insert(newChild); if(tree[nodeIndex].indexOfMaxValue == oldMaxIndex){ if(oldMaxValue < newMaxValue){ tree[nodeIndex].maxValue = newMaxValue; if(tree[nodeIndex].parent != nodeIndex) updateNewMax(tree[nodeIndex].parent, nodeIndex, oldMaxIndex, newMaxIndex, oldMaxValue - tree[nodeIndex].costToParent, newMaxValue - tree[nodeIndex].costToParent); } else if (oldMaxValue > newMaxValue){ long long oldNodeMaxValue = tree[nodeIndex].maxValue; child maxChild = *tree[nodeIndex].children.begin(); if(tree[nodeIndex].children.size() > 1 && maxChild.indexOfMaxValue == oldMaxIndex) maxChild = (*(++tree[nodeIndex].children.begin())); if(maxChild.maxValue > oldNodeMaxValue || (maxChild.maxValue == oldNodeMaxValue && maxChild.indexOfMaxValue < oldMaxIndex)){ tree[nodeIndex].maxValue = maxChild.maxValue; tree[nodeIndex].indexOfMaxValue = maxChild.indexOfMaxValue; if(tree[nodeIndex].parent != nodeIndex) updateNewMax(tree[nodeIndex].parent, nodeIndex, oldMaxIndex, maxChild.indexOfMaxValue, oldMaxValue - tree[nodeIndex].costToParent, maxChild.maxValue - tree[nodeIndex].costToParent); } else{ tree[nodeIndex].maxValue = newMaxValue; if(tree[nodeIndex].parent != nodeIndex) updateNewMax(tree[nodeIndex].parent, nodeIndex, oldMaxIndex, newMaxIndex, oldMaxValue - tree[nodeIndex].costToParent, newMaxValue - tree[nodeIndex].costToParent); } } } else if(tree[nodeIndex].maxValue < newMaxValue){ int oldNodeMaxIndex = tree[nodeIndex].indexOfMaxValue; long long oldNodeMaxValue = tree[nodeIndex].maxValue; tree[nodeIndex].indexOfMaxValue = newMaxIndex; tree[nodeIndex].maxValue = newMaxValue; if(tree[nodeIndex].parent != nodeIndex) updateNewMax(tree[nodeIndex].parent, nodeIndex, oldNodeMaxIndex, newMaxIndex, oldNodeMaxValue - tree[nodeIndex].costToParent, newMaxValue - tree[nodeIndex].costToParent); } else if(tree[nodeIndex].maxValue == newMaxValue && newMaxIndex < tree[nodeIndex].indexOfMaxValue){ int oldNodeMaxIndex = tree[nodeIndex].indexOfMaxValue; tree[nodeIndex].indexOfMaxValue = newMaxIndex; if(tree[nodeIndex].parent != nodeIndex) updateNewMax(tree[nodeIndex].parent, nodeIndex, oldNodeMaxIndex, newMaxIndex, tree[nodeIndex].maxValue - tree[nodeIndex].costToParent, newMaxValue - tree[nodeIndex].costToParent); } } pair<int, long long> searchForMaxDay(int nodeIndex){ if(nodeIndex == -1) return make_pair(-1, LONG_LONG_MIN); pair<int , long long> bottom = searchForBottomMaxDay(nodeIndex); pair<int , long long> top = searchForUpperMaxDay(nodeIndex); pair<int, long long> center = make_pair(tree[nodeIndex].indexOfMaxValue, tree[nodeIndex].maxValue); if(bottom.second > top.second || (bottom.second == top.second && bottom.first < top.first) ){ if(bottom.second > center.second || (bottom.second == center.second && bottom.first < center.first) ) return bottom; else return center; } else{ if(top.second > center.second || (top.second == center.second && top.first < center.first) ) return top; else return center; } } pair<int, long long> searchForBottomMaxDay(int nodeIndex){ if(nodeIndex == -1) return make_pair(-1, LONG_LONG_MIN); if (tree[nodeIndex].indexOfMaxValue != previousDayNode) return make_pair(tree[nodeIndex].indexOfMaxValue, tree[nodeIndex].maxValue); if (tree[nodeIndex].children.size() == 0) { return make_pair(-1, LONG_LONG_MIN); } else if(tree[nodeIndex].children.size() == 1){ pair<int, long long> foundCandidate = searchForBottomMaxDay((*tree[nodeIndex].children.begin()).indexOfChild); if(foundCandidate.first != -1) foundCandidate.second -= tree[(*tree[nodeIndex].children.begin()).indexOfChild].costToParent; return foundCandidate; } else { pair<int, int> firstCandidate = searchForBottomMaxDay((*tree[nodeIndex].children.begin()).indexOfChild); child secondCandidate = *(++tree[nodeIndex].children.begin()); if(firstCandidate.first != -1) firstCandidate.second -= tree[(*tree[nodeIndex].children.begin()).indexOfChild].costToParent; if(firstCandidate.first == -1) return make_pair(secondCandidate.indexOfMaxValue, secondCandidate.maxValue); else if(firstCandidate.second == secondCandidate.maxValue){ if(secondCandidate.indexOfMaxValue < firstCandidate.first) return make_pair(secondCandidate.indexOfMaxValue, secondCandidate.maxValue); else return firstCandidate; } else{ if(secondCandidate.maxValue < firstCandidate.second) return make_pair(secondCandidate.indexOfMaxValue, secondCandidate.maxValue); else return firstCandidate; } } } pair<int, long long> searchForUpperMaxDay(int nodeIndex){ if(nodeIndex == -1) return make_pair(-1, LONG_LONG_MIN); if (tree[nodeIndex].parent == nodeIndex) { return make_pair(-1, LONG_LONG_MIN); } pair<int, long long> foundPair = make_pair(-1, LONG_LONG_MIN); long long penalty = 0; int checkedNode = nodeIndex; while (checkedNode != 0) { penalty += tree[checkedNode].costToParent; checkedNode = tree[checkedNode].parent; if(tree[checkedNode].indexOfMaxValue != previousDayNode){ if((tree[checkedNode].maxValue - penalty) > foundPair.second || ((tree[checkedNode].maxValue - penalty) == foundPair.second && tree[checkedNode].indexOfMaxValue < foundPair.first)){ foundPair.first = tree[checkedNode].indexOfMaxValue; foundPair.second = tree[checkedNode].maxValue - penalty; } } else if(tree[checkedNode].children.size() >= 2){ child candidate = *tree[nodeIndex].children.begin(); if(candidate.indexOfMaxValue == previousDayNode) candidate = *(++tree[nodeIndex].children.begin()); candidate.maxValue -= penalty; if(candidate.maxValue > foundPair.second || (candidate.maxValue == foundPair.second && candidate.indexOfMaxValue < foundPair.first)){ foundPair.first = candidate.indexOfMaxValue; foundPair.second =candidate.maxValue; } } } return foundPair; }
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | #include <stack> #include <vector> #include <set> #include <iostream> #include <climits> #include <stack> using namespace std; struct child { int indexOfChild; long long maxValue; int indexOfMaxValue; bool operator < (const child& ch) const { if(maxValue == ch.maxValue) return (indexOfMaxValue < ch.indexOfMaxValue); else return (maxValue > ch.maxValue); } }; struct node { int parent = -1; long long costToParent; long long value; int indexOfMaxValue = -1; long long maxValue = LONG_LONG_MIN; //Set - maxymalne wartosci w danym poddrzewie set<child> children; }; node tree[100001]; int n, q; int previousDayNode = -1; void buildTree(); void updateNode(int nIndex, long long newValue); void updatePath(int nIndex, long long newPathCost); void updateNewMax(int nodeIndex, int childIndex, int oldMaxIndex, int newMaxIndex, long long oldMaxValue, long long newMaxValue); //maxNode, maxValue pair<int, long long> searchForMaxDay(int nodeIndex); pair<int, long long> searchForBottomMaxDay(int nodeIndex); pair<int, long long> searchForUpperMaxDay(int nodeIndex); int main() { ios_base::sync_with_stdio(0); cin >> n >> q; buildTree(); for(int i = 0; i < q; i++){ int type; cin >> type; if(type == 1){ int node; long long newValue; cin >> node >> newValue; node--; updateNode(node, newValue); } else{ int nodeA, nodeB; long long newCost; cin >> nodeA >> nodeB >> newCost; if (tree[nodeA].parent == nodeB) updatePath(nodeA, newCost); else updatePath(nodeB, newCost); } pair<int, long long> newDay; if(previousDayNode == -1) newDay = searchForMaxDay(0); else newDay = searchForMaxDay(previousDayNode); previousDayNode = newDay.first; cout << newDay.first + 1 << endl; } return 0; } void buildTree(){ vector<int> values(n); vector<bool> wasntChecked(n); vector< vector< pair<int, int> > > connections (n); stack<int> checked; for (int i = 0; i < n; i++) { cin >> values[i]; wasntChecked[i] = true; } for(int i = 0; i < n - 1; i++){ int a, b; long long cost; cin >> a >> b >> cost; a--; b--; connections[a].push_back(make_pair(b, cost)); connections[b].push_back(make_pair(a, cost)); } tree[0].parent = 0; tree[0].costToParent = LONG_LONG_MAX; checked.push(0); while (!checked.empty()) { int index = checked.top(); checked.pop(); wasntChecked[index] = false; for (int i = 0; i < connections[index].size(); i++) { if(wasntChecked[connections[index][i].first]){ tree[connections[index][i].first].parent = index; tree[connections[index][i].first].costToParent = connections[index][i].second; checked.push(connections[index][i].first); } } } for (int i = 0; i < n; i++) updateNode(i, values[i]); } void updateNode(int nIndex, long long newValue){ tree[nIndex].value = newValue; long long oldMaxValue = tree[nIndex].maxValue; int oldMaxIndex = tree[nIndex].indexOfMaxValue; if(newValue > oldMaxValue){ tree[nIndex].maxValue = newValue; tree[nIndex].indexOfMaxValue = nIndex; //Przekazujemy do gory if(tree[nIndex].parent != nIndex) updateNewMax(tree[nIndex].parent, nIndex, oldMaxIndex, nIndex, oldMaxValue - tree[nIndex].costToParent, newValue - tree[nIndex].costToParent); } else if(newValue == oldMaxValue && nIndex < oldMaxIndex){ tree[nIndex].indexOfMaxValue = nIndex; if(tree[nIndex].parent != nIndex) updateNewMax(tree[nIndex].parent, nIndex, oldMaxIndex, nIndex, oldMaxValue - tree[nIndex].costToParent, newValue - tree[nIndex].costToParent); } else if(nIndex == oldMaxIndex){ child maxChild = *tree[nIndex].children.begin(); if(maxChild.maxValue > newValue){ tree[nIndex].indexOfMaxValue = maxChild.indexOfMaxValue; tree[nIndex].maxValue = maxChild.maxValue; if(tree[nIndex].parent != nIndex) updateNewMax(tree[nIndex].parent, nIndex, oldMaxIndex, maxChild.indexOfMaxValue, oldMaxValue - tree[nIndex].costToParent, maxChild.maxValue - tree[nIndex].costToParent); } else if(maxChild.maxValue == newValue && maxChild.indexOfMaxValue < nIndex){ tree[nIndex].indexOfMaxValue = maxChild.indexOfMaxValue; tree[nIndex].maxValue = maxChild.maxValue; if(tree[nIndex].parent != nIndex) updateNewMax(tree[nIndex].parent, nIndex, oldMaxIndex, maxChild.indexOfMaxValue, oldMaxValue - tree[nIndex].costToParent, maxChild.maxValue - tree[nIndex].costToParent); } else if(maxChild.maxValue < newValue){ tree[nIndex].maxValue = newValue; if(tree[nIndex].parent != nIndex) updateNewMax(tree[nIndex].parent, nIndex, oldMaxIndex, nIndex, oldMaxValue - tree[nIndex].costToParent, newValue - tree[nIndex].costToParent); } } } void updatePath(int nIndex, long long newPathCost){ long long oldPathCost = tree[nIndex].costToParent; tree[nIndex].costToParent = newPathCost; if(tree[nIndex].parent != nIndex) updateNewMax(tree[nIndex].parent, nIndex, tree[nIndex].indexOfMaxValue, tree[nIndex].indexOfMaxValue, tree[nIndex].maxValue - oldPathCost, tree[nIndex].maxValue - newPathCost); } void updateNewMax(int nodeIndex, int childIndex, int oldMaxIndex, int newMaxIndex, long long oldMaxValue, long long newMaxValue){ if(nodeIndex == -1 || childIndex == -1 || oldMaxIndex == -1 || newMaxIndex == -1) return; child oldChild, newChild; oldChild.indexOfChild = childIndex; oldChild.indexOfMaxValue = oldMaxIndex; oldChild.maxValue = oldMaxValue; newChild.indexOfChild = childIndex; newChild.indexOfMaxValue = newMaxIndex; newChild.maxValue = newMaxValue; tree[nodeIndex].children.erase(oldChild); tree[nodeIndex].children.insert(newChild); if(tree[nodeIndex].indexOfMaxValue == oldMaxIndex){ if(oldMaxValue < newMaxValue){ tree[nodeIndex].maxValue = newMaxValue; if(tree[nodeIndex].parent != nodeIndex) updateNewMax(tree[nodeIndex].parent, nodeIndex, oldMaxIndex, newMaxIndex, oldMaxValue - tree[nodeIndex].costToParent, newMaxValue - tree[nodeIndex].costToParent); } else if (oldMaxValue > newMaxValue){ long long oldNodeMaxValue = tree[nodeIndex].maxValue; child maxChild = *tree[nodeIndex].children.begin(); if(tree[nodeIndex].children.size() > 1 && maxChild.indexOfMaxValue == oldMaxIndex) maxChild = (*(++tree[nodeIndex].children.begin())); if(maxChild.maxValue > oldNodeMaxValue || (maxChild.maxValue == oldNodeMaxValue && maxChild.indexOfMaxValue < oldMaxIndex)){ tree[nodeIndex].maxValue = maxChild.maxValue; tree[nodeIndex].indexOfMaxValue = maxChild.indexOfMaxValue; if(tree[nodeIndex].parent != nodeIndex) updateNewMax(tree[nodeIndex].parent, nodeIndex, oldMaxIndex, maxChild.indexOfMaxValue, oldMaxValue - tree[nodeIndex].costToParent, maxChild.maxValue - tree[nodeIndex].costToParent); } else{ tree[nodeIndex].maxValue = newMaxValue; if(tree[nodeIndex].parent != nodeIndex) updateNewMax(tree[nodeIndex].parent, nodeIndex, oldMaxIndex, newMaxIndex, oldMaxValue - tree[nodeIndex].costToParent, newMaxValue - tree[nodeIndex].costToParent); } } } else if(tree[nodeIndex].maxValue < newMaxValue){ int oldNodeMaxIndex = tree[nodeIndex].indexOfMaxValue; long long oldNodeMaxValue = tree[nodeIndex].maxValue; tree[nodeIndex].indexOfMaxValue = newMaxIndex; tree[nodeIndex].maxValue = newMaxValue; if(tree[nodeIndex].parent != nodeIndex) updateNewMax(tree[nodeIndex].parent, nodeIndex, oldNodeMaxIndex, newMaxIndex, oldNodeMaxValue - tree[nodeIndex].costToParent, newMaxValue - tree[nodeIndex].costToParent); } else if(tree[nodeIndex].maxValue == newMaxValue && newMaxIndex < tree[nodeIndex].indexOfMaxValue){ int oldNodeMaxIndex = tree[nodeIndex].indexOfMaxValue; tree[nodeIndex].indexOfMaxValue = newMaxIndex; if(tree[nodeIndex].parent != nodeIndex) updateNewMax(tree[nodeIndex].parent, nodeIndex, oldNodeMaxIndex, newMaxIndex, tree[nodeIndex].maxValue - tree[nodeIndex].costToParent, newMaxValue - tree[nodeIndex].costToParent); } } pair<int, long long> searchForMaxDay(int nodeIndex){ if(nodeIndex == -1) return make_pair(-1, LONG_LONG_MIN); pair<int , long long> bottom = searchForBottomMaxDay(nodeIndex); pair<int , long long> top = searchForUpperMaxDay(nodeIndex); pair<int, long long> center = make_pair(tree[nodeIndex].indexOfMaxValue, tree[nodeIndex].maxValue); if(bottom.second > top.second || (bottom.second == top.second && bottom.first < top.first) ){ if(bottom.second > center.second || (bottom.second == center.second && bottom.first < center.first) ) return bottom; else return center; } else{ if(top.second > center.second || (top.second == center.second && top.first < center.first) ) return top; else return center; } } pair<int, long long> searchForBottomMaxDay(int nodeIndex){ if(nodeIndex == -1) return make_pair(-1, LONG_LONG_MIN); if (tree[nodeIndex].indexOfMaxValue != previousDayNode) return make_pair(tree[nodeIndex].indexOfMaxValue, tree[nodeIndex].maxValue); if (tree[nodeIndex].children.size() == 0) { return make_pair(-1, LONG_LONG_MIN); } else if(tree[nodeIndex].children.size() == 1){ pair<int, long long> foundCandidate = searchForBottomMaxDay((*tree[nodeIndex].children.begin()).indexOfChild); if(foundCandidate.first != -1) foundCandidate.second -= tree[(*tree[nodeIndex].children.begin()).indexOfChild].costToParent; return foundCandidate; } else { pair<int, int> firstCandidate = searchForBottomMaxDay((*tree[nodeIndex].children.begin()).indexOfChild); child secondCandidate = *(++tree[nodeIndex].children.begin()); if(firstCandidate.first != -1) firstCandidate.second -= tree[(*tree[nodeIndex].children.begin()).indexOfChild].costToParent; if(firstCandidate.first == -1) return make_pair(secondCandidate.indexOfMaxValue, secondCandidate.maxValue); else if(firstCandidate.second == secondCandidate.maxValue){ if(secondCandidate.indexOfMaxValue < firstCandidate.first) return make_pair(secondCandidate.indexOfMaxValue, secondCandidate.maxValue); else return firstCandidate; } else{ if(secondCandidate.maxValue < firstCandidate.second) return make_pair(secondCandidate.indexOfMaxValue, secondCandidate.maxValue); else return firstCandidate; } } } pair<int, long long> searchForUpperMaxDay(int nodeIndex){ if(nodeIndex == -1) return make_pair(-1, LONG_LONG_MIN); if (tree[nodeIndex].parent == nodeIndex) { return make_pair(-1, LONG_LONG_MIN); } pair<int, long long> foundPair = make_pair(-1, LONG_LONG_MIN); long long penalty = 0; int checkedNode = nodeIndex; while (checkedNode != 0) { penalty += tree[checkedNode].costToParent; checkedNode = tree[checkedNode].parent; if(tree[checkedNode].indexOfMaxValue != previousDayNode){ if((tree[checkedNode].maxValue - penalty) > foundPair.second || ((tree[checkedNode].maxValue - penalty) == foundPair.second && tree[checkedNode].indexOfMaxValue < foundPair.first)){ foundPair.first = tree[checkedNode].indexOfMaxValue; foundPair.second = tree[checkedNode].maxValue - penalty; } } else if(tree[checkedNode].children.size() >= 2){ child candidate = *tree[nodeIndex].children.begin(); if(candidate.indexOfMaxValue == previousDayNode) candidate = *(++tree[nodeIndex].children.begin()); candidate.maxValue -= penalty; if(candidate.maxValue > foundPair.second || (candidate.maxValue == foundPair.second && candidate.indexOfMaxValue < foundPair.first)){ foundPair.first = candidate.indexOfMaxValue; foundPair.second =candidate.maxValue; } } } return foundPair; } |