#include <bits/stdc++.h> #define rep(a, b) for(int a = 0; a < b; a++) #define pb push_back #define var(a, b) __typeof(b) a = b #define foreach(a, b) for(var(a, (b).begin()); a != (b).end(); a++) using namespace std; typedef long long ll; vector<int> odp; ll maks, akt; const ll inf = 1e18; template<class V, class E>struct Graph{ struct Ed: E{ int v; Ed(int w, E e): v(w), E(e) {} }; struct Ve: vector<Ed>, V{}; vector<Ve> g; Graph(int n = 0): g(n) {} void EdgeU(int a, int b, E e = E()){ g[a].pb(Ed(b, e)); g[b].pb(Ed(a, e)); } void DfsR(int v){ if(v != akt && ((maks < g[v].val + g[v].t) || (maks == g[v].val + g[v].t && v < akt))){ maks = g[v].val + g[v].t; akt = v; } foreach(it, g[v]) if(g[it->v].t == -1){ g[it->v].t = g[v].t + it->l; DfsR(it->v); } } void Dfs(int v){ rep(i, g.size()) g[i].t = -1; g[v].t = 0; DfsR(v); } void Change(int a, int b, E e = E()){ foreach(it, g[a]) if(it->v == b){ it->l = e.l; break; } foreach(it, g[b]) if(it->v == a){ it->l = e.l; break; } } }; struct Vv{ ll val; ll t; }; struct Ee{ ll l; }; int main(){ ios_base::sync_with_stdio(0); ll n, q; cin >> n >> q; Graph<Vv, Ee> g(n); ll a, b; Ee eg; rep(i, n) cin >> g.g[i].val; rep(i, n-1){ cin >> a >> b >> eg.l; eg.l *= -1; g.EdgeU(a-1, b-1, eg); } ll op; akt = 0; rep(i, q){ maks = -inf; cin >> op; if(op == 1){ cin >> a >> b; g.g[a-1].val = b; } else{ cin >> a >> b >> eg.l; eg.l *= -1; g.Change(a-1, b-1, eg); } g.Dfs(akt); odp.pb(akt); } rep(i, odp.size()) cout << odp[i]+1 << " "; }
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 | #include <bits/stdc++.h> #define rep(a, b) for(int a = 0; a < b; a++) #define pb push_back #define var(a, b) __typeof(b) a = b #define foreach(a, b) for(var(a, (b).begin()); a != (b).end(); a++) using namespace std; typedef long long ll; vector<int> odp; ll maks, akt; const ll inf = 1e18; template<class V, class E>struct Graph{ struct Ed: E{ int v; Ed(int w, E e): v(w), E(e) {} }; struct Ve: vector<Ed>, V{}; vector<Ve> g; Graph(int n = 0): g(n) {} void EdgeU(int a, int b, E e = E()){ g[a].pb(Ed(b, e)); g[b].pb(Ed(a, e)); } void DfsR(int v){ if(v != akt && ((maks < g[v].val + g[v].t) || (maks == g[v].val + g[v].t && v < akt))){ maks = g[v].val + g[v].t; akt = v; } foreach(it, g[v]) if(g[it->v].t == -1){ g[it->v].t = g[v].t + it->l; DfsR(it->v); } } void Dfs(int v){ rep(i, g.size()) g[i].t = -1; g[v].t = 0; DfsR(v); } void Change(int a, int b, E e = E()){ foreach(it, g[a]) if(it->v == b){ it->l = e.l; break; } foreach(it, g[b]) if(it->v == a){ it->l = e.l; break; } } }; struct Vv{ ll val; ll t; }; struct Ee{ ll l; }; int main(){ ios_base::sync_with_stdio(0); ll n, q; cin >> n >> q; Graph<Vv, Ee> g(n); ll a, b; Ee eg; rep(i, n) cin >> g.g[i].val; rep(i, n-1){ cin >> a >> b >> eg.l; eg.l *= -1; g.EdgeU(a-1, b-1, eg); } ll op; akt = 0; rep(i, q){ maks = -inf; cin >> op; if(op == 1){ cin >> a >> b; g.g[a-1].val = b; } else{ cin >> a >> b >> eg.l; eg.l *= -1; g.Change(a-1, b-1, eg); } g.Dfs(akt); odp.pb(akt); } rep(i, odp.size()) cout << odp[i]+1 << " "; } |