#include <iostream> #include <vector> #include <climits> using namespace std; #define SIZE 100000 struct Pair{ long long first, second; Pair(long long a, long long b){ first = a; second = b; } }; struct Node{ long long val; vector<Pair> neighbours; }; Node cities[SIZE]; int n, q; int visited[SIZE]; long long length[SIZE]; void DFS(int curr, long long wayLength){ visited[curr]=1; length[curr]+=cities[curr].val; for(int i=0; i<cities[curr].neighbours.size(); i++){ if(!visited[cities[curr].neighbours[i].first]){ length[cities[curr].neighbours[i].first] -= wayLength + cities[curr].neighbours[i].second; DFS(cities[curr].neighbours[i].first, wayLength + cities[curr].neighbours[i].second); } } } int main(){ cin >> n >> q; for(int i=0; i<n; i++){ cin >> cities[i].val; } for(int i=0; i<n-1; i++){ long long a, b, c; cin >> a >> b >> c; cities[a-1].neighbours.push_back(Pair(b-1,c)); cities[b-1].neighbours.push_back(Pair(a-1,c)); } int current=0; for(int i=0; i<q; i++){ int opt; cin >> opt; if(opt == 1){ long long v, d; cin >> v >> d; cities[v-1].val=d; } else { long long a, b, c; cin >> a >> b >> c; for(int j=0; j<cities[a-1].neighbours.size(); j++){ if(cities[a-1].neighbours[j].first == b-1) { cities[a-1].neighbours[j].second = c; break; } } for(int j=0; j<cities[b-1].neighbours.size(); j++){ if(cities[b-1].neighbours[j].first == a-1) { cities[b-1].neighbours[j].second = c; break; } } } for(int i=0; i<n; i++){ visited[i]=0; length[i]=0; } DFS(current, 0); int max=INT_MIN, next; for(int i=0; i<n; i++){ if(i != current && length[i] >= max){ if(!(length[i] == max && i > next)){ next = i; max = length[i]; } } } cout << next+1 << " "; current = next; } cout << "\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 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 | #include <iostream> #include <vector> #include <climits> using namespace std; #define SIZE 100000 struct Pair{ long long first, second; Pair(long long a, long long b){ first = a; second = b; } }; struct Node{ long long val; vector<Pair> neighbours; }; Node cities[SIZE]; int n, q; int visited[SIZE]; long long length[SIZE]; void DFS(int curr, long long wayLength){ visited[curr]=1; length[curr]+=cities[curr].val; for(int i=0; i<cities[curr].neighbours.size(); i++){ if(!visited[cities[curr].neighbours[i].first]){ length[cities[curr].neighbours[i].first] -= wayLength + cities[curr].neighbours[i].second; DFS(cities[curr].neighbours[i].first, wayLength + cities[curr].neighbours[i].second); } } } int main(){ cin >> n >> q; for(int i=0; i<n; i++){ cin >> cities[i].val; } for(int i=0; i<n-1; i++){ long long a, b, c; cin >> a >> b >> c; cities[a-1].neighbours.push_back(Pair(b-1,c)); cities[b-1].neighbours.push_back(Pair(a-1,c)); } int current=0; for(int i=0; i<q; i++){ int opt; cin >> opt; if(opt == 1){ long long v, d; cin >> v >> d; cities[v-1].val=d; } else { long long a, b, c; cin >> a >> b >> c; for(int j=0; j<cities[a-1].neighbours.size(); j++){ if(cities[a-1].neighbours[j].first == b-1) { cities[a-1].neighbours[j].second = c; break; } } for(int j=0; j<cities[b-1].neighbours.size(); j++){ if(cities[b-1].neighbours[j].first == a-1) { cities[b-1].neighbours[j].second = c; break; } } } for(int i=0; i<n; i++){ visited[i]=0; length[i]=0; } DFS(current, 0); int max=INT_MIN, next; for(int i=0; i<n; i++){ if(i != current && length[i] >= max){ if(!(length[i] == max && i > next)){ next = i; max = length[i]; } } } cout << next+1 << " "; current = next; } cout << "\n"; } |