#include"bits/stdtr1c++.h"
using namespace std;
long long a,b,c,d,e,f,t;
long long tab[100001];
bool odw[100001];
vector <vector <pair <int, long long> > > graf;
stack <pair <int ,long long > > q;
int dfs(int start)
{
for(int i=1;i<=a;i++)
{
odw[i]=0;
}
odw[start]=1;
f=-1000000000000000000;
q.push({start,0});
while(!q.empty())
{
long long cost=q.top().second;
int v=q.top().first;
q.pop();
for(auto u:graf[v])
{
if(!odw[u.first])
{
odw[u.first]=1;
if(tab[u.first]-cost-u.second>f)
{
e=u.first;
f=tab[u.first]-cost-u.second;
}
else if(tab[u.first]-cost-u.second==f)
{
if(u.first<e)
{
e=u.first;
f=tab[u.first]-cost-u.second;
}
}
q.push({u.first,cost+u.second});
}
}
}
t=e;
return e;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin>>a>>b;
graf.resize(a+1);
for(int i=1;i<=a;i++)
{
cin>>tab[i];
}
for(int i=1;i<a;i++)
{
cin>>c>>d>>e;
graf[c].push_back({d,e});
graf[d].push_back({c,e});
}
t=1;
for(int i=0;i<b;i++)
{
cin>>c;
if(c==1)
{
cin>>c>>d;
tab[c]=d;
}
else
{
cin>>c>>d>>e;
for(int i=0;i<graf[c].size();i++)
{
if(graf[c][i].first==d)
{
graf[c][i].second=e;
break;
}
}
for(int i=0;i<graf[d].size();i++)
{
if(graf[d][i].first==c)
{
graf[d][i].second=e;
break;
}
}
}
cout<<dfs(t)<<' ';
}
}
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 | #include"bits/stdtr1c++.h" using namespace std; long long a,b,c,d,e,f,t; long long tab[100001]; bool odw[100001]; vector <vector <pair <int, long long> > > graf; stack <pair <int ,long long > > q; int dfs(int start) { for(int i=1;i<=a;i++) { odw[i]=0; } odw[start]=1; f=-1000000000000000000; q.push({start,0}); while(!q.empty()) { long long cost=q.top().second; int v=q.top().first; q.pop(); for(auto u:graf[v]) { if(!odw[u.first]) { odw[u.first]=1; if(tab[u.first]-cost-u.second>f) { e=u.first; f=tab[u.first]-cost-u.second; } else if(tab[u.first]-cost-u.second==f) { if(u.first<e) { e=u.first; f=tab[u.first]-cost-u.second; } } q.push({u.first,cost+u.second}); } } } t=e; return e; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin>>a>>b; graf.resize(a+1); for(int i=1;i<=a;i++) { cin>>tab[i]; } for(int i=1;i<a;i++) { cin>>c>>d>>e; graf[c].push_back({d,e}); graf[d].push_back({c,e}); } t=1; for(int i=0;i<b;i++) { cin>>c; if(c==1) { cin>>c>>d; tab[c]=d; } else { cin>>c>>d>>e; for(int i=0;i<graf[c].size();i++) { if(graf[c][i].first==d) { graf[c][i].second=e; break; } } for(int i=0;i<graf[d].size();i++) { if(graf[d][i].first==c) { graf[d][i].second=e; break; } } } cout<<dfs(t)<<' '; } } |
English