#include <bits/stdc++.h> using namespace std; vector<long long> buyout; map<pair<int, int>, long long> dist; vector<vector<int>> g; long long res, tRes, id; int me = 0; void dfs(int s, int p) { if(s != me) { if(tRes+buyout[s] > res || (tRes+buyout[s] == res && s < id)) { res = tRes+buyout[s]; id = s; } } for(auto I : g[s]) { if(I != p) { tRes -= dist[{s, I}]; dfs(I, s); tRes += dist[{s, I}]; } } } int main() { int n, q, a, b, querry; long long c; scanf("%d%d", &n, &q); g.resize(n); buyout.resize(n); for(auto &I : buyout) scanf("%lld", &I); for(int i = 0;i < n-1;++i) { scanf("%d%d %lld", &a, &b, &c); --a;--b; dist[{a, b}] = c; dist[{b, a}] = c; g[a].push_back(b); g[b].push_back(a); } while(q--) { scanf("%d", &querry); if(querry == 1) { scanf("%d %lld", &a, &c); --a; buyout[a] = c; } else { scanf("%d %d %lld", &a, &b, &c); --a;--b; dist[{a, b}] = c; dist[{b, a}] = c; } res = -1e18; tRes = 0; id = 1e9; dfs(me, -1); me = id; printf("%d ", me+1); } printf("\n"); 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 | #include <bits/stdc++.h> using namespace std; vector<long long> buyout; map<pair<int, int>, long long> dist; vector<vector<int>> g; long long res, tRes, id; int me = 0; void dfs(int s, int p) { if(s != me) { if(tRes+buyout[s] > res || (tRes+buyout[s] == res && s < id)) { res = tRes+buyout[s]; id = s; } } for(auto I : g[s]) { if(I != p) { tRes -= dist[{s, I}]; dfs(I, s); tRes += dist[{s, I}]; } } } int main() { int n, q, a, b, querry; long long c; scanf("%d%d", &n, &q); g.resize(n); buyout.resize(n); for(auto &I : buyout) scanf("%lld", &I); for(int i = 0;i < n-1;++i) { scanf("%d%d %lld", &a, &b, &c); --a;--b; dist[{a, b}] = c; dist[{b, a}] = c; g[a].push_back(b); g[b].push_back(a); } while(q--) { scanf("%d", &querry); if(querry == 1) { scanf("%d %lld", &a, &c); --a; buyout[a] = c; } else { scanf("%d %d %lld", &a, &b, &c); --a;--b; dist[{a, b}] = c; dist[{b, a}] = c; } res = -1e18; tRes = 0; id = 1e9; dfs(me, -1); me = id; printf("%d ", me+1); } printf("\n"); return 0; } |