#include <bits/stdc++.h> #define M make_pair #define int long long using namespace std; vector<vector<pair<int,long long> > > graf; vector<long long> koszt; vector<bool> visited; pair<long long,int> dfs(int cur){ // cout<<cur<<"->"; visited[cur] = true; pair<long long,int> best = M(koszt[cur],cur); for(int p=0;p<graf[cur].size();p++){ pair<long long,long long> a = graf[cur][p]; if(visited[a.first] == false){ pair<long long,long long> r1 = dfs(a.first); if(r1.first-a.second > best.first || (r1.first - a.second == best.first && a.first < best.first)){ best = r1; best.first-=a.second; } } } return best; } int32_t main(){ ios::sync_with_stdio(false); int n,q; cin >> n >> q; graf.resize(n); koszt.resize(n); visited.resize(n); for(int i=0;i<n;i++){ cin >> koszt[i]; } for(int i=0;i<n-1;i++){ long long a,b,c; cin >> a>>b >>c; graf[a-1].push_back(M(b-1,c)); graf[b-1].push_back(M(a-1,c)); } long long prev = 0; // koszt[prev] = -1e18-5; for(int i=0;i<q;i++){ //cout<<"Q1"<<endl; long long a,b,c,d; cin >> d >> a>> b; if(d == 1){ koszt[a-1] = b; } else{ cin >> c; for(int j=0;j<graf[a-1].size();j++){ if(graf[a-1][j].first == b-1){ graf[a-1][j].second = c; break; } } } visited.clear(); visited.resize(n); long long cp = koszt[prev]; long long cp2 = prev; koszt[prev] = -1e18-5; //cout<<prev+1<<" A "; //cout<<koszt[0]<<endl; prev = dfs(prev).second; koszt[cp2] = cp; // koszt[prev] = -1e18-5; cout<<prev+1<<" "; } }
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 | #include <bits/stdc++.h> #define M make_pair #define int long long using namespace std; vector<vector<pair<int,long long> > > graf; vector<long long> koszt; vector<bool> visited; pair<long long,int> dfs(int cur){ // cout<<cur<<"->"; visited[cur] = true; pair<long long,int> best = M(koszt[cur],cur); for(int p=0;p<graf[cur].size();p++){ pair<long long,long long> a = graf[cur][p]; if(visited[a.first] == false){ pair<long long,long long> r1 = dfs(a.first); if(r1.first-a.second > best.first || (r1.first - a.second == best.first && a.first < best.first)){ best = r1; best.first-=a.second; } } } return best; } int32_t main(){ ios::sync_with_stdio(false); int n,q; cin >> n >> q; graf.resize(n); koszt.resize(n); visited.resize(n); for(int i=0;i<n;i++){ cin >> koszt[i]; } for(int i=0;i<n-1;i++){ long long a,b,c; cin >> a>>b >>c; graf[a-1].push_back(M(b-1,c)); graf[b-1].push_back(M(a-1,c)); } long long prev = 0; // koszt[prev] = -1e18-5; for(int i=0;i<q;i++){ //cout<<"Q1"<<endl; long long a,b,c,d; cin >> d >> a>> b; if(d == 1){ koszt[a-1] = b; } else{ cin >> c; for(int j=0;j<graf[a-1].size();j++){ if(graf[a-1][j].first == b-1){ graf[a-1][j].second = c; break; } } } visited.clear(); visited.resize(n); long long cp = koszt[prev]; long long cp2 = prev; koszt[prev] = -1e18-5; //cout<<prev+1<<" A "; //cout<<koszt[0]<<endl; prev = dfs(prev).second; koszt[cp2] = cp; // koszt[prev] = -1e18-5; cout<<prev+1<<" "; } } |