#include <bits/stdc++.h> using namespace std; typedef long long int64; #define DEBUG(x) cerr << #x << " = " << x << endl; #define REP(x, n) for(__typeof(n) x = 0; x < (n); ++x) #define FOR(x, b, e) for(__typeof(b) x = (b); x != (e); x += 1 - 2 * ((b) > (e))) const int64 INF = 1000000000000000001; const double EPS = 10e-9; template<class V, class E> struct Graph { struct Edge : E { int v; Edge(E d, int w): E(d), v(w) { } }; struct Vertex : V, vector<Edge> { }; vector<Vertex> g; Graph(int n) : g(n) { } void edgeD(int b, int e, E d = E()) { g[b].push_back(Edge(d, e)); } void edgeU(int b, int e, E d = E()) { Edge eg(d, e); eg.rev = g[e].size(); g[b].push_back(eg); eg.v = b; eg.rev = g[b].size() - 1; g[e].push_back(eg); } void write() { REP(x, g.size()) { cout << "<Vector(id=" << x << ", d=" << g[x].d << ", t=" << g[x].t << ")>: "; for (auto it : g[x]) cout << it.v << "(" << it.l << ") "; cout << "\n"; } } void costs_dfs(int v) { for (auto it : g[v]) { if (g[it.v].t == -1) { g[it.v].t = g[v].t + it.l; costs_dfs(it.v); } } } void costs(int s) { for (auto& v : g) { v.t = -1; } g[s].t = 0; costs_dfs(s); } int best(int s) { costs(s); int64 maxi = -INF; int best = -1; int n = (int)g.size(); REP(x, n) { if (x != s) { int64 m = g[x].d - g[x].t; if (m > maxi) { maxi = m; best = x; } } } assert(best != -1); return best; } }; struct Vs { int64 d, t; }; struct Ve { size_t rev; int64 l; }; #ifndef CATCH_TEST int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; Graph<Vs, Ve> g(n); REP(x, n) { cin >> g.g[x].d; } REP(x, n - 1) { int b, e; Ve ed; cin >> b >> e >> ed.l; g.edgeU(b - 1, e - 1, ed); } int last = 0; REP(x, q) { int t; cin >> t; if (t == 1) { int v; int64 d; cin >> v >> d; g.g[v - 1].d = d; } else { int b, e; int64 l; cin >> b >> e >> l; for (auto& ed : g.g[b - 1]) { if (ed.v == e - 1) { ed.l = l; g.g[e - 1][ed.rev].l = l; } } } last = g.best(last); cout << last + 1 << " "; } cout << endl; return 0; } #endif
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | #include <bits/stdc++.h> using namespace std; typedef long long int64; #define DEBUG(x) cerr << #x << " = " << x << endl; #define REP(x, n) for(__typeof(n) x = 0; x < (n); ++x) #define FOR(x, b, e) for(__typeof(b) x = (b); x != (e); x += 1 - 2 * ((b) > (e))) const int64 INF = 1000000000000000001; const double EPS = 10e-9; template<class V, class E> struct Graph { struct Edge : E { int v; Edge(E d, int w): E(d), v(w) { } }; struct Vertex : V, vector<Edge> { }; vector<Vertex> g; Graph(int n) : g(n) { } void edgeD(int b, int e, E d = E()) { g[b].push_back(Edge(d, e)); } void edgeU(int b, int e, E d = E()) { Edge eg(d, e); eg.rev = g[e].size(); g[b].push_back(eg); eg.v = b; eg.rev = g[b].size() - 1; g[e].push_back(eg); } void write() { REP(x, g.size()) { cout << "<Vector(id=" << x << ", d=" << g[x].d << ", t=" << g[x].t << ")>: "; for (auto it : g[x]) cout << it.v << "(" << it.l << ") "; cout << "\n"; } } void costs_dfs(int v) { for (auto it : g[v]) { if (g[it.v].t == -1) { g[it.v].t = g[v].t + it.l; costs_dfs(it.v); } } } void costs(int s) { for (auto& v : g) { v.t = -1; } g[s].t = 0; costs_dfs(s); } int best(int s) { costs(s); int64 maxi = -INF; int best = -1; int n = (int)g.size(); REP(x, n) { if (x != s) { int64 m = g[x].d - g[x].t; if (m > maxi) { maxi = m; best = x; } } } assert(best != -1); return best; } }; struct Vs { int64 d, t; }; struct Ve { size_t rev; int64 l; }; #ifndef CATCH_TEST int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; Graph<Vs, Ve> g(n); REP(x, n) { cin >> g.g[x].d; } REP(x, n - 1) { int b, e; Ve ed; cin >> b >> e >> ed.l; g.edgeU(b - 1, e - 1, ed); } int last = 0; REP(x, q) { int t; cin >> t; if (t == 1) { int v; int64 d; cin >> v >> d; g.g[v - 1].d = d; } else { int b, e; int64 l; cin >> b >> e >> l; for (auto& ed : g.g[b - 1]) { if (ed.v == e - 1) { ed.l = l; g.g[e - 1][ed.rev].l = l; } } } last = g.best(last); cout << last + 1 << " "; } cout << endl; return 0; } #endif |