#include <bits/stdc++.h> #define st first #define nd second #define pb push_back using namespace std; const int MAX_N = 1e5 + 9; typedef pair<int, int> PII; typedef long long lli; pair<PII, lli> E[MAX_N]; lli W[MAX_N]; vector<int> G[MAX_N]; int been[MAX_N]; int zero = 0; int root = 0; int n, q; inline int other(int v, int e) { return E[e].st.st == v ? E[e].st.nd : E[e].st.st; } pair<lli, int> DFS(int v, lli curr = 0, pair<lli, int> maxx = { -LLONG_MAX, -1 } ) { been[v] = zero + 1; if(v != root) maxx = max(maxx, { W[v] - curr, v } ); for(auto e : G[v]) { int u = other(v, e); lli d = E[e].nd; if(been[u] <= zero) { maxx = max(maxx, DFS(u, curr + d, maxx)); } } return maxx; } int main() { scanf("%d %d", &n, &q); for(int i = 1; i <= n; ++i) { scanf("%lld", &W[i]); } int v, u; lli w; for(int i = 1; i < n; ++i) { scanf("%d %d %lld", &v, &u, &w); E[i] = { { v, u }, w }; G[v].pb(i); G[u].pb(i); } int ctrl; int curr = 1; while(q --> 0) { scanf("%d", &ctrl); if(ctrl == 1) { scanf("%d %lld", &v, &w); W[v] = w; } else { scanf("%d %d %lld", &v, &u, &w); for(auto e : G[v]) { if(other(v, e) == u) { E[e].nd = w; } } for(auto e : G[u]) { if(other(u, e) == v) { E[e].nd = w; } } } ++zero; root = curr; curr = DFS(curr).nd; printf("%d ", curr); } 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 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 | #include <bits/stdc++.h> #define st first #define nd second #define pb push_back using namespace std; const int MAX_N = 1e5 + 9; typedef pair<int, int> PII; typedef long long lli; pair<PII, lli> E[MAX_N]; lli W[MAX_N]; vector<int> G[MAX_N]; int been[MAX_N]; int zero = 0; int root = 0; int n, q; inline int other(int v, int e) { return E[e].st.st == v ? E[e].st.nd : E[e].st.st; } pair<lli, int> DFS(int v, lli curr = 0, pair<lli, int> maxx = { -LLONG_MAX, -1 } ) { been[v] = zero + 1; if(v != root) maxx = max(maxx, { W[v] - curr, v } ); for(auto e : G[v]) { int u = other(v, e); lli d = E[e].nd; if(been[u] <= zero) { maxx = max(maxx, DFS(u, curr + d, maxx)); } } return maxx; } int main() { scanf("%d %d", &n, &q); for(int i = 1; i <= n; ++i) { scanf("%lld", &W[i]); } int v, u; lli w; for(int i = 1; i < n; ++i) { scanf("%d %d %lld", &v, &u, &w); E[i] = { { v, u }, w }; G[v].pb(i); G[u].pb(i); } int ctrl; int curr = 1; while(q --> 0) { scanf("%d", &ctrl); if(ctrl == 1) { scanf("%d %lld", &v, &w); W[v] = w; } else { scanf("%d %d %lld", &v, &u, &w); for(auto e : G[v]) { if(other(v, e) == u) { E[e].nd = w; } } for(auto e : G[u]) { if(other(u, e) == v) { E[e].nd = w; } } } ++zero; root = curr; curr = DFS(curr).nd; printf("%d ", curr); } printf("\n"); return 0; } |