#include <cstdio> #include <vector> #define REP(i, n) for (int i = 0; i < (n); ++i) #define LL long long #define PII pair<int, int> #define MP make_pair #define FI first #define SE second using namespace std; const int maxn = 100010; int n, q; LL z[maxn]; vector<pair<int, LL> > g[maxn]; int s; int ch; bool vis[maxn]; LL res; void traverse(int v, LL sum) { vis[v] = true; if (v != s && (sum + z[v] > res || (sum + z[v] == res && v < ch))) { res = sum + z[v]; ch = v; } REP(i, g[v].size()) { if (!vis[g[v][i].FI]) { traverse(g[v][i].FI, sum - g[v][i].SE); } } } int main() { scanf("%d%d", &n, &q); REP(i, n) { scanf("%lld", &z[i]); } REP(i, n - 1) { int a, b; LL c; scanf("%d%d%lld", &a, &b, &c); --a; --b; g[a].push_back(MP(b, c)); g[b].push_back(MP(a, c)); } REP(i, q) { int type; scanf("%d", &type); if (type == 1) { int v; LL d; scanf("%d%lld", &v, &d); --v; z[v] = d; } else { int a, b; LL d; scanf("%d%d%lld", &a, &b, &d); --a; --b; REP(j, g[a].size()) { if (g[a][j].FI == b) { g[a][j].SE = d; break; } } REP(j, g[b].size()) { if (g[b][j].FI == a) { g[b][j].SE = d; break; } } } REP(j, n) { vis[j] = false; } res = -2000L; REP(j, 5) { res *= 1000L; } traverse(s, 0); if (i > 0) { printf(" "); } printf("%d", ch + 1); s = ch; } 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 | #include <cstdio> #include <vector> #define REP(i, n) for (int i = 0; i < (n); ++i) #define LL long long #define PII pair<int, int> #define MP make_pair #define FI first #define SE second using namespace std; const int maxn = 100010; int n, q; LL z[maxn]; vector<pair<int, LL> > g[maxn]; int s; int ch; bool vis[maxn]; LL res; void traverse(int v, LL sum) { vis[v] = true; if (v != s && (sum + z[v] > res || (sum + z[v] == res && v < ch))) { res = sum + z[v]; ch = v; } REP(i, g[v].size()) { if (!vis[g[v][i].FI]) { traverse(g[v][i].FI, sum - g[v][i].SE); } } } int main() { scanf("%d%d", &n, &q); REP(i, n) { scanf("%lld", &z[i]); } REP(i, n - 1) { int a, b; LL c; scanf("%d%d%lld", &a, &b, &c); --a; --b; g[a].push_back(MP(b, c)); g[b].push_back(MP(a, c)); } REP(i, q) { int type; scanf("%d", &type); if (type == 1) { int v; LL d; scanf("%d%lld", &v, &d); --v; z[v] = d; } else { int a, b; LL d; scanf("%d%d%lld", &a, &b, &d); --a; --b; REP(j, g[a].size()) { if (g[a][j].FI == b) { g[a][j].SE = d; break; } } REP(j, g[b].size()) { if (g[b][j].FI == a) { g[b][j].SE = d; break; } } } REP(j, n) { vis[j] = false; } res = -2000L; REP(j, 5) { res *= 1000L; } traverse(s, 0); if (i > 0) { printf(" "); } printf("%d", ch + 1); s = ch; } printf("\n"); return 0; } |