#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct miasto{
long long int zysk;
long long int odl;
bool odwiedzony;
vector<pair<int, long long int> > k;
};
bool comp(pair<int, long long int> a, pair<int, long long int>b){
if(a.second == b.second){
return a.first<b.first;
}
return a.second>b.second;
}
void dfs(miasto tree[], int start, long long int d){
tree[start].odl = d;
tree[start].odwiedzony = true;
for(auto it = tree[start].k.begin(); it != tree[start].k.end(); it++){
// cout<<it->first<<"\n";
if(tree[it->first].odwiedzony == false) {
dfs(tree, it->first, it->second + d);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
int n,q;
long long int z;
cin>>n>>q;
miasto tree[n];
vector <pair<int, long long int> > res;
int pozycja = 0;
for(int i=0;i<n;i++){
cin>>z;
tree[i].zysk = z;
tree[i].odwiedzony = false;
}
int a,b;
for(int i=0;i<n - 1;i++){
cin>>a>>b>>z;
a--;
b--;
tree[a].k.push_back(make_pair(b, z));
tree[b].k.push_back(make_pair(a, z));
}
for(int i=0;i<q;i++){
cin>>a;
if(a == 1){
cin>>a>>z;
a--;
tree[a].zysk = z;
}
else {
cin >> a >> b >> z;
a--;
b--;
for (auto it = tree[a].k.begin(); it != tree[a].k.end(); it++) {
if (it->first == b) {
it->second = z;
break;
}
}
for (auto it = tree[b].k.begin(); it != tree[b].k.end(); it++) {
if (it->first == a) {
it->second = z;
break;
}
}
}
dfs(tree, pozycja, 0);
for(int i2=0;i2<n;i2++)
{
tree[i2].odwiedzony = false;
if(i2!=pozycja){
res.push_back(make_pair(i2, tree[i2].zysk - tree[i2].odl));
}
}
sort(res.begin(), res.end(), comp);
pozycja = res.begin()->first;
/* for(auto it = res.begin();it!=res.end();it++){
cout<<it->first<<" "<<it->second<<" ";
}*/
cout<<res.begin()->first + 1<<" ";
res.clear();
}
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | #include <iostream> #include <vector> #include <algorithm> using namespace std; struct miasto{ long long int zysk; long long int odl; bool odwiedzony; vector<pair<int, long long int> > k; }; bool comp(pair<int, long long int> a, pair<int, long long int>b){ if(a.second == b.second){ return a.first<b.first; } return a.second>b.second; } void dfs(miasto tree[], int start, long long int d){ tree[start].odl = d; tree[start].odwiedzony = true; for(auto it = tree[start].k.begin(); it != tree[start].k.end(); it++){ // cout<<it->first<<"\n"; if(tree[it->first].odwiedzony == false) { dfs(tree, it->first, it->second + d); } } } int main() { ios_base::sync_with_stdio(0); int n,q; long long int z; cin>>n>>q; miasto tree[n]; vector <pair<int, long long int> > res; int pozycja = 0; for(int i=0;i<n;i++){ cin>>z; tree[i].zysk = z; tree[i].odwiedzony = false; } int a,b; for(int i=0;i<n - 1;i++){ cin>>a>>b>>z; a--; b--; tree[a].k.push_back(make_pair(b, z)); tree[b].k.push_back(make_pair(a, z)); } for(int i=0;i<q;i++){ cin>>a; if(a == 1){ cin>>a>>z; a--; tree[a].zysk = z; } else { cin >> a >> b >> z; a--; b--; for (auto it = tree[a].k.begin(); it != tree[a].k.end(); it++) { if (it->first == b) { it->second = z; break; } } for (auto it = tree[b].k.begin(); it != tree[b].k.end(); it++) { if (it->first == a) { it->second = z; break; } } } dfs(tree, pozycja, 0); for(int i2=0;i2<n;i2++) { tree[i2].odwiedzony = false; if(i2!=pozycja){ res.push_back(make_pair(i2, tree[i2].zysk - tree[i2].odl)); } } sort(res.begin(), res.end(), comp); pozycja = res.begin()->first; /* for(auto it = res.begin();it!=res.end();it++){ cout<<it->first<<" "<<it->second<<" "; }*/ cout<<res.begin()->first + 1<<" "; res.clear(); } return 0; } |
English