// brute O(q*n) solution #include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> using namespace std; #define pb push_back #define mp make_pair #define pii pair<int,int> #define fi first #define se second #define vi vector<int> #define vpii vector<pii> #define SZ(x) ((int)(x.size())) #define DBG cerr << "debug here" << endl; #define DBGV(vari) cerr << #vari<< " = "<< (vari) <<endl; typedef long long ll; const int INF = 1e9; const int N = 1e5+10; vector<pair<int, ll>> g[N]; ll value[N]; int newRoot; ll bestCost; void dfs(int v, int curRoot, int p, ll acc) { if (v != curRoot and (newRoot == -1 or mp(acc + value[v], -v) > mp(bestCost, -newRoot))) { newRoot = v; bestCost = acc + value[v]; } for (int i = 0; i < g[v].size(); ++i) { int u = g[v][i].fi; if (u == p) continue; dfs(u, v, v, acc-g[v][i].se); } } int main() { ios_base::sync_with_stdio(0); int n, q; scanf("%d %d", &n, &q); for (int i = 0; i < n; ++i) { scanf("%lld", &value[i]); } for (int i = 0; i < n-1; ++i) { int v, u; ll c; scanf("%d %d %lld", &v, &u, &c); --v, --u; if (v > u) swap(v,u); g[v].pb({u, c}); g[u].pb({v, c}); } int curRoot = 0; for (int qid = 0; qid < q; ++qid) { int qtype; scanf("%d", &qtype); if (qtype == 1) { int v; ll newValue; scanf("%d %lld", &v, &newValue); --v; value[v] = newValue; } else if (qtype == 2) { int v, u; ll newCost; scanf("%d %d %lld", &v, &u, &newCost); --v, --u; if (v > u) swap(v,u); for (int i = 0; i < g[v].size(); ++i) { if (g[v][i].fi == u) { g[v][i].se = newCost; break; } } for (int i = 0; i < g[u].size(); ++i) { if (g[u][i].fi == v) { g[u][i].se = newCost; break; } } } else assert(false); newRoot = -1; bestCost = 0; dfs(curRoot, curRoot, -1, 0); curRoot = newRoot; if (qid == q-1) { printf("%d", newRoot+1); } else { printf("%d ", newRoot+1); } } 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 | // brute O(q*n) solution #include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> using namespace std; #define pb push_back #define mp make_pair #define pii pair<int,int> #define fi first #define se second #define vi vector<int> #define vpii vector<pii> #define SZ(x) ((int)(x.size())) #define DBG cerr << "debug here" << endl; #define DBGV(vari) cerr << #vari<< " = "<< (vari) <<endl; typedef long long ll; const int INF = 1e9; const int N = 1e5+10; vector<pair<int, ll>> g[N]; ll value[N]; int newRoot; ll bestCost; void dfs(int v, int curRoot, int p, ll acc) { if (v != curRoot and (newRoot == -1 or mp(acc + value[v], -v) > mp(bestCost, -newRoot))) { newRoot = v; bestCost = acc + value[v]; } for (int i = 0; i < g[v].size(); ++i) { int u = g[v][i].fi; if (u == p) continue; dfs(u, v, v, acc-g[v][i].se); } } int main() { ios_base::sync_with_stdio(0); int n, q; scanf("%d %d", &n, &q); for (int i = 0; i < n; ++i) { scanf("%lld", &value[i]); } for (int i = 0; i < n-1; ++i) { int v, u; ll c; scanf("%d %d %lld", &v, &u, &c); --v, --u; if (v > u) swap(v,u); g[v].pb({u, c}); g[u].pb({v, c}); } int curRoot = 0; for (int qid = 0; qid < q; ++qid) { int qtype; scanf("%d", &qtype); if (qtype == 1) { int v; ll newValue; scanf("%d %lld", &v, &newValue); --v; value[v] = newValue; } else if (qtype == 2) { int v, u; ll newCost; scanf("%d %d %lld", &v, &u, &newCost); --v, --u; if (v > u) swap(v,u); for (int i = 0; i < g[v].size(); ++i) { if (g[v][i].fi == u) { g[v][i].se = newCost; break; } } for (int i = 0; i < g[u].size(); ++i) { if (g[u][i].fi == v) { g[u][i].se = newCost; break; } } } else assert(false); newRoot = -1; bestCost = 0; dfs(curRoot, curRoot, -1, 0); curRoot = newRoot; if (qid == q-1) { printf("%d", newRoot+1); } else { printf("%d ", newRoot+1); } } printf("\n"); return 0; } |