#include <bits/stdc++.h>
#define ll long long int
#define inf (ll)0xfffffffffffffff
using namespace std;
vector<ll> val;
vector<ll> dis;
map<pair<int, int>, int> KrawToID;
vector<vector<pair<int, int> > > Tree;
vector<bool> was;
ll best = -inf;
int current = 1;
int last = 1;
int n, q;
void dfs(int x,ll depth) {
if (was[x])
{
return;
}
was[x] = true;
if (last != x && val[x] - depth >= best)
{
if (val[x] - depth== best)
{
current = min(x, current);
}
else
{
best = val[x] - depth;
current = x;
}
}
for (auto y : Tree[x]) {
dfs(y.first, depth + dis[y.second]);
}
}
ll RunQuery() {
best = -inf;
was.clear();
was.resize(n + 1);
dfs(last, 0);
last = current;
return current;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
val.resize(n + 1);
dis.resize(n + 1);
Tree.resize(n + 1);
for (int i = 0; i < n; i++)
{
cin >> val[i+1];
}
for (int i = 0; i < n-1; i++)
{
ll a, b, c;
cin >> a >> b >> c;
if (a>b)
{
swap(a, b);
}
KrawToID[{a, b}] = i+1;
Tree[a].push_back({ b,i+1 });
Tree[b].push_back({ a,i+1 });
dis[i+1] = c;
}
for (int i = 0; i < q; i++)
{
int T;
cin >> T;
if (T==1)
{
ll v, d;
cin >> v>> d;
val[v] = d;
}
else
{
ll a, b, d;
cin >> a >> b >> d;
if (a>b)
{
swap(a, b);
}
dis[KrawToID[{a, b}]] = d;
}
cout << RunQuery() << " ";
}
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 | #include <bits/stdc++.h> #define ll long long int #define inf (ll)0xfffffffffffffff using namespace std; vector<ll> val; vector<ll> dis; map<pair<int, int>, int> KrawToID; vector<vector<pair<int, int> > > Tree; vector<bool> was; ll best = -inf; int current = 1; int last = 1; int n, q; void dfs(int x,ll depth) { if (was[x]) { return; } was[x] = true; if (last != x && val[x] - depth >= best) { if (val[x] - depth== best) { current = min(x, current); } else { best = val[x] - depth; current = x; } } for (auto y : Tree[x]) { dfs(y.first, depth + dis[y.second]); } } ll RunQuery() { best = -inf; was.clear(); was.resize(n + 1); dfs(last, 0); last = current; return current; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> q; val.resize(n + 1); dis.resize(n + 1); Tree.resize(n + 1); for (int i = 0; i < n; i++) { cin >> val[i+1]; } for (int i = 0; i < n-1; i++) { ll a, b, c; cin >> a >> b >> c; if (a>b) { swap(a, b); } KrawToID[{a, b}] = i+1; Tree[a].push_back({ b,i+1 }); Tree[b].push_back({ a,i+1 }); dis[i+1] = c; } for (int i = 0; i < q; i++) { int T; cin >> T; if (T==1) { ll v, d; cin >> v>> d; val[v] = d; } else { ll a, b, d; cin >> a >> b >> d; if (a>b) { swap(a, b); } dis[KrawToID[{a, b}]] = d; } cout << RunQuery() << " "; } return 0; } |
English