#include<bits/stdc++.h> using namespace std; #define f first #define s second #define PB push_back typedef long long ll; typedef long double ld; #define REP(x, y) for(int x=0;x<(y);x++) #define ROF(x, y) for(int x=(y);x>=0;x--) #define FOR(x, z, y) for(int x=(z);x<=(y);x++) #define INT(x) int x;scanf("%d",&x) #define LL(x) ll x;scanf("%lld",&x) #define CZ(x) char x;scanf(" %c",&x) typedef pair<int, ll> pil; vector<pil> tos[100005]; ll t[100005]; bool vis[100005]; ll wyn = LLONG_MIN; int where = 0; void dfs(int x, ll koszt) { if (koszt != 0 && (t[x] - koszt > wyn || (t[x]-koszt == wyn && x <where))) { where = x; wyn = t[x] - koszt; } vis[x] = true; ROF(i, tos[x].size() - 1) { if (!vis[tos[x][i].f]) { dfs(tos[x][i].f, koszt + tos[x][i].s); } } } int main() { INT(n); INT(q); REP(i, n) { LL(a); t[i] = a; } REP(i, n - 1) { INT(a); INT(b); LL(x); tos[a - 1].PB(pil(b - 1, x)); tos[b - 1].PB(pil(a - 1, x)); } while (q--) { INT(x); if (x == 1) { INT(a); INT(b); t[a - 1] = b; } else { INT(a); INT(b); INT(c); tos[a - 1].PB(pil(b - 1, c)); tos[b - 1].PB(pil(a - 1, c)); } dfs(where, 0); printf("%d ", where + 1); REP(i, n)vis[i] = false; wyn = LLONG_MIN; } }
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 | #include<bits/stdc++.h> using namespace std; #define f first #define s second #define PB push_back typedef long long ll; typedef long double ld; #define REP(x, y) for(int x=0;x<(y);x++) #define ROF(x, y) for(int x=(y);x>=0;x--) #define FOR(x, z, y) for(int x=(z);x<=(y);x++) #define INT(x) int x;scanf("%d",&x) #define LL(x) ll x;scanf("%lld",&x) #define CZ(x) char x;scanf(" %c",&x) typedef pair<int, ll> pil; vector<pil> tos[100005]; ll t[100005]; bool vis[100005]; ll wyn = LLONG_MIN; int where = 0; void dfs(int x, ll koszt) { if (koszt != 0 && (t[x] - koszt > wyn || (t[x]-koszt == wyn && x <where))) { where = x; wyn = t[x] - koszt; } vis[x] = true; ROF(i, tos[x].size() - 1) { if (!vis[tos[x][i].f]) { dfs(tos[x][i].f, koszt + tos[x][i].s); } } } int main() { INT(n); INT(q); REP(i, n) { LL(a); t[i] = a; } REP(i, n - 1) { INT(a); INT(b); LL(x); tos[a - 1].PB(pil(b - 1, x)); tos[b - 1].PB(pil(a - 1, x)); } while (q--) { INT(x); if (x == 1) { INT(a); INT(b); t[a - 1] = b; } else { INT(a); INT(b); INT(c); tos[a - 1].PB(pil(b - 1, c)); tos[b - 1].PB(pil(a - 1, c)); } dfs(where, 0); printf("%d ", where + 1); REP(i, n)vis[i] = false; wyn = LLONG_MIN; } } |