#include <cstdio>
#include <vector>
#include <stdint.h>
using namespace std;
int n, q;
vector<uint64_t> V;
vector<vector<pair<int, uint64_t> > > A;
int main(){
scanf("%d %d", &n, &q);
V.resize(n); A.resize(n);
for (int i=0; i<n; ++i) scanf("%llu", &V[i]);
for (int i=0; i<n-1; ++i) {
uint32_t a, b;
uint64_t z;
scanf("%d %d %llu", &a, &b, &z); --a; --b;
A[a].push_back(make_pair(b, z));
A[b].push_back(make_pair(a, z));
}
int current_pos = 0;
for (int i=0; i<q; ++i) {
uint32_t c, a, b;
uint64_t d;
scanf("%d", &c);
if (c == 1) {
scanf("%d %llu", &a, &d); --a;
V[a] = d;
} else {
scanf("%d %d %llu", &a, &b, &d); --a; --b;
for (int j=0; j<A[a].size(); ++j) if (A[a][j].first == b){ A[a][j] = make_pair(b, d); break; }
for (int j=0; j<A[b].size(); ++j) if (A[b][j].first == a){ A[b][j] = make_pair(a, d); break; }
}
vector<bool> used(n, false);
vector<pair<int, uint64_t> > Q;
Q.push_back(make_pair(current_pos, 0));
int64_t best_res = -5000000000000000000;
uint32_t best_pos = -1;
while (!Q.empty()) {
int v_pos = Q.back().first;
int64_t v_res = Q.back().second;
used[v_pos] = true;
Q.pop_back();
if (v_pos != current_pos) {
int64_t possible_res = V[v_pos] - v_res;
if (possible_res > best_res || (possible_res == best_res && v_pos < best_pos)) {
best_res = possible_res;
best_pos = v_pos;
}
}
for(int i=0; i<A[v_pos].size(); ++i) {
if (used[A[v_pos][i].first]) continue;
Q.push_back(make_pair(A[v_pos][i].first, v_res + A[v_pos][i].second));
}
}
current_pos = best_pos;
printf("%d ", best_pos + 1);
}
printf("\n");
}
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 | #include <cstdio> #include <vector> #include <stdint.h> using namespace std; int n, q; vector<uint64_t> V; vector<vector<pair<int, uint64_t> > > A; int main(){ scanf("%d %d", &n, &q); V.resize(n); A.resize(n); for (int i=0; i<n; ++i) scanf("%llu", &V[i]); for (int i=0; i<n-1; ++i) { uint32_t a, b; uint64_t z; scanf("%d %d %llu", &a, &b, &z); --a; --b; A[a].push_back(make_pair(b, z)); A[b].push_back(make_pair(a, z)); } int current_pos = 0; for (int i=0; i<q; ++i) { uint32_t c, a, b; uint64_t d; scanf("%d", &c); if (c == 1) { scanf("%d %llu", &a, &d); --a; V[a] = d; } else { scanf("%d %d %llu", &a, &b, &d); --a; --b; for (int j=0; j<A[a].size(); ++j) if (A[a][j].first == b){ A[a][j] = make_pair(b, d); break; } for (int j=0; j<A[b].size(); ++j) if (A[b][j].first == a){ A[b][j] = make_pair(a, d); break; } } vector<bool> used(n, false); vector<pair<int, uint64_t> > Q; Q.push_back(make_pair(current_pos, 0)); int64_t best_res = -5000000000000000000; uint32_t best_pos = -1; while (!Q.empty()) { int v_pos = Q.back().first; int64_t v_res = Q.back().second; used[v_pos] = true; Q.pop_back(); if (v_pos != current_pos) { int64_t possible_res = V[v_pos] - v_res; if (possible_res > best_res || (possible_res == best_res && v_pos < best_pos)) { best_res = possible_res; best_pos = v_pos; } } for(int i=0; i<A[v_pos].size(); ++i) { if (used[A[v_pos][i].first]) continue; Q.push_back(make_pair(A[v_pos][i].first, v_res + A[v_pos][i].second)); } } current_pos = best_pos; printf("%d ", best_pos + 1); } printf("\n"); } |
English