#include <set>
#include <unordered_map>
#include <vector>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
typedef long long int LL;
const LL N = 1e6+5;
const LL INF = 9e18;
struct Vertex
{
LL earn;
vector<int> edges;
};
Vertex v[N];
unordered_map<LL, LL> m;
bool odw[N];
int main()
{
int n, q, a, b, d;
LL c;
scanf("%d%d", &n, &q);
for(int i = 1; i <= n; ++i)
scanf("%lld", &v[i].earn);
for(int i = 0; i < n-1; ++i)
{
scanf("%d%d%lld", &a, &b, &c);
v[a].edges.push_back(b);
v[b].edges.push_back(a);
m[a*N+b] = c;
m[b*N+a] = c;
}
int current = 1;
for(int i = 0; i < q; ++i)
{
scanf("%d", &d);
if(d == 1)
{
scanf("%d%lld", &a, &c);
v[a].earn = c;
}
else
{
scanf("%d%d%lld", &a, &b, &c);
m[a*N+b] = c;
m[b*N+a] = c;
}
memset(odw, 0, n+2);
queue<pair<int, LL>> que;
que.push(make_pair(current, 0));
odw[current] = true;
LL maxi = -INF;
int maxv = 1000000000;
while(!que.empty())
{
pair<int, LL> curr = que.front();
que.pop();
for(int next : v[curr.first].edges)
{
if(odw[next]) continue;
odw[next] = true;
LL cost = v[next].earn-(curr.second+m[next*N+curr.first]);
if(cost > maxi || (cost == maxi && next < maxv))
{
maxv = next;
maxi = cost;
}
que.push(make_pair(next, cost-v[next].earn));
}
}
printf("%d ", maxv);
current = maxv;
}
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 | #include <set> #include <unordered_map> #include <vector> #include <cstdio> #include <queue> #include <cstring> using namespace std; typedef long long int LL; const LL N = 1e6+5; const LL INF = 9e18; struct Vertex { LL earn; vector<int> edges; }; Vertex v[N]; unordered_map<LL, LL> m; bool odw[N]; int main() { int n, q, a, b, d; LL c; scanf("%d%d", &n, &q); for(int i = 1; i <= n; ++i) scanf("%lld", &v[i].earn); for(int i = 0; i < n-1; ++i) { scanf("%d%d%lld", &a, &b, &c); v[a].edges.push_back(b); v[b].edges.push_back(a); m[a*N+b] = c; m[b*N+a] = c; } int current = 1; for(int i = 0; i < q; ++i) { scanf("%d", &d); if(d == 1) { scanf("%d%lld", &a, &c); v[a].earn = c; } else { scanf("%d%d%lld", &a, &b, &c); m[a*N+b] = c; m[b*N+a] = c; } memset(odw, 0, n+2); queue<pair<int, LL>> que; que.push(make_pair(current, 0)); odw[current] = true; LL maxi = -INF; int maxv = 1000000000; while(!que.empty()) { pair<int, LL> curr = que.front(); que.pop(); for(int next : v[curr.first].edges) { if(odw[next]) continue; odw[next] = true; LL cost = v[next].earn-(curr.second+m[next*N+curr.first]); if(cost > maxi || (cost == maxi && next < maxv)) { maxv = next; maxi = cost; } que.push(make_pair(next, cost-v[next].earn)); } } printf("%d ", maxv); current = maxv; } return 0; } |
English