#include<bits/stdc++.h> using namespace std; const int N = 100003; const long long INF = 1e18; vector<pair<int, long long> > v[N]; bool o[N]; int miasto = N, akt = 1; long long maks = -INF, z[N]; void dfs(int x, long long zysk) { o[x] = 1; zysk += z[x]; if(x!=akt) if(maks<zysk or (maks==zysk and x<miasto) ) { miasto = x; maks = zysk; } for(auto i: v[x]) if(!o[i.first]) dfs(i.first, zysk-z[x]-i.second); } int main() { int n, q; scanf("%d%d", &n, &q); for(int i = 1; i <= n; i++) scanf("%lld", &z[i]); for(int i = 0; i < n-1; i++) { int a, b; long long c; scanf("%d%d%lld", &a, &b, &c); v[a].push_back(make_pair(b, c)); v[b].push_back(make_pair(a, c)); } for(int i = 1; i <= n; i++) sort(v[i].begin(), v[i].end()); for(int i = 0; i < q; i++) { int a, b; long long c, d; scanf("%d%d%lld", &a, &b, &c); if(a==1) z[b] = c; else { scanf("%lld", &d); int p = 0, k = v[b].size()-1; while(p<k) { int sr = (p+k)/2; if(v[b][sr].first<c) p = sr+1; else k = sr; } v[b][p].second = d; p = 0, k = v[c].size()-1; while(p<k) { int sr = (p+k)/2; if(v[c][sr].first<b) p = sr+1; else k = sr; } v[c][p].second = d; } dfs(akt, -z[akt]); printf("%d ", miasto); for(int j = 1; j <= n; j++) o[j] = 0; akt = miasto; maks = -INF; miasto = 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 | #include<bits/stdc++.h> using namespace std; const int N = 100003; const long long INF = 1e18; vector<pair<int, long long> > v[N]; bool o[N]; int miasto = N, akt = 1; long long maks = -INF, z[N]; void dfs(int x, long long zysk) { o[x] = 1; zysk += z[x]; if(x!=akt) if(maks<zysk or (maks==zysk and x<miasto) ) { miasto = x; maks = zysk; } for(auto i: v[x]) if(!o[i.first]) dfs(i.first, zysk-z[x]-i.second); } int main() { int n, q; scanf("%d%d", &n, &q); for(int i = 1; i <= n; i++) scanf("%lld", &z[i]); for(int i = 0; i < n-1; i++) { int a, b; long long c; scanf("%d%d%lld", &a, &b, &c); v[a].push_back(make_pair(b, c)); v[b].push_back(make_pair(a, c)); } for(int i = 1; i <= n; i++) sort(v[i].begin(), v[i].end()); for(int i = 0; i < q; i++) { int a, b; long long c, d; scanf("%d%d%lld", &a, &b, &c); if(a==1) z[b] = c; else { scanf("%lld", &d); int p = 0, k = v[b].size()-1; while(p<k) { int sr = (p+k)/2; if(v[b][sr].first<c) p = sr+1; else k = sr; } v[b][p].second = d; p = 0, k = v[c].size()-1; while(p<k) { int sr = (p+k)/2; if(v[c][sr].first<b) p = sr+1; else k = sr; } v[c][p].second = d; } dfs(akt, -z[akt]); printf("%d ", miasto); for(int j = 1; j <= n; j++) o[j] = 0; akt = miasto; maks = -INF; miasto = N; } } |