#include <iostream> #include <algorithm> #include <vector> #include <cstdio> using namespace std; typedef long long ll; typedef pair<ll, ll> pii; const ll maxN = 100000; const ll INF = 9223372036854775807; ll prof[maxN+1]; vector<ll> E[maxN+1]; vector<ll> C[maxN+1]; pii dfs(ll v, ll pred, ll myCosts, ll a, ll b, ll c, bool starter) { pii res; if(!starter) { res = pii(prof[v]-myCosts, -v); } else { res = pii(-INF, -v); //cout << INF <<"\n"; } for(ll i=0; i<E[v].size(); i++) { ll nei = E[v][i]; if(nei == b && v == a) { C[v][i] = c; } if(nei == a && v == b) { C[v][i] = c; } if(nei == pred) { continue; } res = max(res, dfs(nei, v, myCosts + C[v][i], a, b, c, false) ); } return res; } int main() { ios_base::sync_with_stdio(0); ll n, q; cin >> n >> q; for(ll i=1; i<=n; i++) { cin >> prof[i]; } ll a, b, c; for(ll i=0; i<n-1; i++) { cin >> a >> b >> c; E[a].push_back(b); C[a].push_back(c); E[b].push_back(a); C[b].push_back(c); } ll orig = 1; while(q--) { ll cmd; cin >> cmd; if(cmd == 1) { ll v, d; cin >> v >> d; prof[v] = d; a = b = c = 0; } else { cin >> a >> b >> c; } pii getter = dfs(orig, 0, 0, a, b, c, true); orig = -getter.second; cout << orig << " "; //<< getter.first << "\n"; } /*for(int i=1; i<=n; i++) { for(int j=0; j<E[i].size(); j++) { cout << C[i][j] << " "; } cout << "\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 110 111 112 113 114 115 116 | #include <iostream> #include <algorithm> #include <vector> #include <cstdio> using namespace std; typedef long long ll; typedef pair<ll, ll> pii; const ll maxN = 100000; const ll INF = 9223372036854775807; ll prof[maxN+1]; vector<ll> E[maxN+1]; vector<ll> C[maxN+1]; pii dfs(ll v, ll pred, ll myCosts, ll a, ll b, ll c, bool starter) { pii res; if(!starter) { res = pii(prof[v]-myCosts, -v); } else { res = pii(-INF, -v); //cout << INF <<"\n"; } for(ll i=0; i<E[v].size(); i++) { ll nei = E[v][i]; if(nei == b && v == a) { C[v][i] = c; } if(nei == a && v == b) { C[v][i] = c; } if(nei == pred) { continue; } res = max(res, dfs(nei, v, myCosts + C[v][i], a, b, c, false) ); } return res; } int main() { ios_base::sync_with_stdio(0); ll n, q; cin >> n >> q; for(ll i=1; i<=n; i++) { cin >> prof[i]; } ll a, b, c; for(ll i=0; i<n-1; i++) { cin >> a >> b >> c; E[a].push_back(b); C[a].push_back(c); E[b].push_back(a); C[b].push_back(c); } ll orig = 1; while(q--) { ll cmd; cin >> cmd; if(cmd == 1) { ll v, d; cin >> v >> d; prof[v] = d; a = b = c = 0; } else { cin >> a >> b >> c; } pii getter = dfs(orig, 0, 0, a, b, c, true); orig = -getter.second; cout << orig << " "; //<< getter.first << "\n"; } /*for(int i=1; i<=n; i++) { for(int j=0; j<E[i].size(); j++) { cout << C[i][j] << " "; } cout << "\n"; }*/ return 0; } |