#include <iostream> #include <map> #include <vector> using namespace std; #define LL long long #define MAX_N 100200 int n, q; LL T[MAX_N]; vector<int> V[MAX_N]; map<pair<int, int>, LL> E; inline LL& EAt(int a, int b) { return E[{min(a, b), max(a, b)}]; } pair<int, LL> best(int cur, int par = -1) { int ret = -1; LL retD = -1; for(int i : V[cur]) { if(i != par) { auto p = best(i, cur); p.second -= EAt(cur, i); if(p.first != -1) { if(ret == -1 || (retD < p.second || (retD == p.second && ret > p.first))) { ret = p.first; retD = p.second; } } if(ret == -1 || (retD < T[i] - EAt(cur, i) || (retD == T[i] - EAt(cur, i) && ret > i))) { ret = i; retD = T[i] - EAt(cur, i); } } } return {ret, retD}; } int main() { ios::sync_with_stdio(0); cin >> n >> q; for(int a = 0; a < n; ++a) { cin >> T[a]; } for(int a = 1; a < n; ++a) { int x, y; LL d; cin >> x >> y >> d; --x; --y; EAt(x, y) = d; V[x].push_back(y); V[y].push_back(x); } int cur = 0; while(q--) { int op; cin >> op; if(op == 1) { int v; LL d; cin >> v >> d; --v; T[v] = d; } else { int a, b; LL d; cin >> a >> b >> d; --a; --b; EAt(a, b) = d; } auto b = best(cur); cur = b.first; cout << cur + 1 << " "; } 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 | #include <iostream> #include <map> #include <vector> using namespace std; #define LL long long #define MAX_N 100200 int n, q; LL T[MAX_N]; vector<int> V[MAX_N]; map<pair<int, int>, LL> E; inline LL& EAt(int a, int b) { return E[{min(a, b), max(a, b)}]; } pair<int, LL> best(int cur, int par = -1) { int ret = -1; LL retD = -1; for(int i : V[cur]) { if(i != par) { auto p = best(i, cur); p.second -= EAt(cur, i); if(p.first != -1) { if(ret == -1 || (retD < p.second || (retD == p.second && ret > p.first))) { ret = p.first; retD = p.second; } } if(ret == -1 || (retD < T[i] - EAt(cur, i) || (retD == T[i] - EAt(cur, i) && ret > i))) { ret = i; retD = T[i] - EAt(cur, i); } } } return {ret, retD}; } int main() { ios::sync_with_stdio(0); cin >> n >> q; for(int a = 0; a < n; ++a) { cin >> T[a]; } for(int a = 1; a < n; ++a) { int x, y; LL d; cin >> x >> y >> d; --x; --y; EAt(x, y) = d; V[x].push_back(y); V[y].push_back(x); } int cur = 0; while(q--) { int op; cin >> op; if(op == 1) { int v; LL d; cin >> v >> d; --v; T[v] = d; } else { int a, b; LL d; cin >> a >> b >> d; --a; --b; EAt(a, b) = d; } auto b = best(cur); cur = b.first; cout << cur + 1 << " "; } return 0; } |