#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, ll> edge; #define X first #define Y second int n, q; ll z[100007]; vector<edge> G[100007]; int city = 1; ll dist[100007]; void dfs(int w) { for(edge u : G[w]) { if(dist[u.X] == -1) { dist[u.X] = dist[w] + u.Y; dfs(u.X); } } } int closest() { memset(dist, -1, sizeof dist); dist[city] = 0; dfs(city); ll best = -1e18; int nekst = -1; for(int i = 1 ; i <= n ; i++) { if(city != i) { if(-dist[i] + z[i] > best) { best = -dist[i] + z[i]; nekst = i; } } } return nekst; } int main() { scanf("%d %d", &n, &q); for(int i = 1 ; i <= n ; i++) scanf("%lld", &z[i]); int a, b; ll c; for(int i = 1 ; i < n ; i++) { scanf("%d %d %lld", &a, &b, &c); G[a].push_back({b, c}); G[b].push_back({a, c}); } for(int i = 1 ; i <= q ; i++) { int t; scanf("%d", &t); if(t == 1) { scanf("%d %lld", &a, &c); z[a] = c; } else { scanf("%d %d %lld", &a, &b, &c); for(int i = 0 ; i < G[a].size() ; i++) if(G[a][i].X == b) G[a][i].Y = c; for(int i = 0 ; i < G[b].size() ; i++) if(G[b][i].X == a) G[b][i].Y = c; } printf("%d ", city = closest()); } 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, ll> edge; #define X first #define Y second int n, q; ll z[100007]; vector<edge> G[100007]; int city = 1; ll dist[100007]; void dfs(int w) { for(edge u : G[w]) { if(dist[u.X] == -1) { dist[u.X] = dist[w] + u.Y; dfs(u.X); } } } int closest() { memset(dist, -1, sizeof dist); dist[city] = 0; dfs(city); ll best = -1e18; int nekst = -1; for(int i = 1 ; i <= n ; i++) { if(city != i) { if(-dist[i] + z[i] > best) { best = -dist[i] + z[i]; nekst = i; } } } return nekst; } int main() { scanf("%d %d", &n, &q); for(int i = 1 ; i <= n ; i++) scanf("%lld", &z[i]); int a, b; ll c; for(int i = 1 ; i < n ; i++) { scanf("%d %d %lld", &a, &b, &c); G[a].push_back({b, c}); G[b].push_back({a, c}); } for(int i = 1 ; i <= q ; i++) { int t; scanf("%d", &t); if(t == 1) { scanf("%d %lld", &a, &c); z[a] = c; } else { scanf("%d %d %lld", &a, &b, &c); for(int i = 0 ; i < G[a].size() ; i++) if(G[a][i].X == b) G[a][i].Y = c; for(int i = 0 ; i < G[b].size() ; i++) if(G[b][i].X == a) G[b][i].Y = c; } printf("%d ", city = closest()); } return 0; } |