#include <iostream> #include <vector> using namespace std; typedef long long LL; const int MAX = 1000001; LL ceny[MAX]; vector <pair <int, LL> > v[MAX]; int akt=1; LL maxi=-1000000000000000001L; int n, q; bool odw[MAX]; void dfs(int s, LL droga){ odw[s]=true; for(unsigned i=0; i<v[s].size(); i++){ if(odw[v[s][i].first]==false){ if(maxi < ceny[v[s][i].first] -v[s][i].second - droga){ maxi = ceny[v[s][i].first] -v[s][i].second - droga; akt = v[s][i].first; } else if(maxi == ceny[v[s][i].first] -v[s][i].second - droga){ if(akt > v[s][i].first){ maxi = ceny[v[s][i].first] -v[s][i].second - droga; akt = v[s][i].first; } } dfs(v[s][i].first, droga + v[s][i].second); } } } int main(){ ios::sync_with_stdio(0); cin>>n>>q; for(int i=1; i<=n; i++) cin>>ceny[i]; for(int i=1; i<n; i++){ int a, b; LL c; cin>>a>>b>>c; v[a].push_back(make_pair(b,c)); v[b].push_back(make_pair(a,c)); } for(int i=0; i<q; i++){ int typ, a, b, c; cin>>typ; if(typ==1){ cin>>a>>c; ceny[a]=c; } else{ cin>>a>>b>>c; for(unsigned i=0; i<v[a].size(); i++){ if(v[a][i].first==b){ v[a][i].second=c; break; } } for(unsigned i=0; i<v[b].size(); i++){ if(v[b][i].first==a){ v[b][i].second=c; break; } } } for(int i=0; i<=n; i++) odw[i]=false; dfs(akt, 0); maxi=-1000000000000000001L; cout<<akt<<" "; } 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 | #include <iostream> #include <vector> using namespace std; typedef long long LL; const int MAX = 1000001; LL ceny[MAX]; vector <pair <int, LL> > v[MAX]; int akt=1; LL maxi=-1000000000000000001L; int n, q; bool odw[MAX]; void dfs(int s, LL droga){ odw[s]=true; for(unsigned i=0; i<v[s].size(); i++){ if(odw[v[s][i].first]==false){ if(maxi < ceny[v[s][i].first] -v[s][i].second - droga){ maxi = ceny[v[s][i].first] -v[s][i].second - droga; akt = v[s][i].first; } else if(maxi == ceny[v[s][i].first] -v[s][i].second - droga){ if(akt > v[s][i].first){ maxi = ceny[v[s][i].first] -v[s][i].second - droga; akt = v[s][i].first; } } dfs(v[s][i].first, droga + v[s][i].second); } } } int main(){ ios::sync_with_stdio(0); cin>>n>>q; for(int i=1; i<=n; i++) cin>>ceny[i]; for(int i=1; i<n; i++){ int a, b; LL c; cin>>a>>b>>c; v[a].push_back(make_pair(b,c)); v[b].push_back(make_pair(a,c)); } for(int i=0; i<q; i++){ int typ, a, b, c; cin>>typ; if(typ==1){ cin>>a>>c; ceny[a]=c; } else{ cin>>a>>b>>c; for(unsigned i=0; i<v[a].size(); i++){ if(v[a][i].first==b){ v[a][i].second=c; break; } } for(unsigned i=0; i<v[b].size(); i++){ if(v[b][i].first==a){ v[b][i].second=c; break; } } } for(int i=0; i<=n; i++) odw[i]=false; dfs(akt, 0); maxi=-1000000000000000001L; cout<<akt<<" "; } return 0; } |