#include <iostream> #include <vector> #include <unordered_map> #include <queue> const int N = 100000; const long long inf = 9e18; using namespace std; long long z[N]; struct edge { int neigh; long long charge; }; struct change_charge { int a; int b; long long charge; }; struct change_gain { int city; long long gain; }; struct toBfs { int nrCity; long long loss; }; vector<edge> country[N+5]; bool visited[N+5]; vector<int> result; unordered_map<int, change_charge> charges; unordered_map<int, change_gain> profits; int main() { std::ios_base::sync_with_stdio(false); int n, q, a, b, v, type; long long c, d; cin >> n >> q; for(int i=1; i<=n; i++) cin >> z[i]; for(int i=1; i<n; i++) { cin >> a >> b >> c; country[a].push_back({b, c}); country[b].push_back({a, c}); } for(int i=1; i<=q; i++) { cin >> type; if(type==1) { cin >> v >> d; profits[i] = {v, d}; } else { cin >> a >> b >> d; charges[i] = {a, b, d}; } } int s = 1; for(int i=1; i<=q; i++) { auto it = profits.find(i); if(it==profits.end()) { auto it2 = charges.find(i); for(auto& child : country[(it2->second).a]) { if(child.neigh==(it2->second).b) child.charge = (it2->second).charge; } for(auto& child : country[(it2->second).b]) { if(child.neigh==(it2->second).a) child.charge = (it2->second).charge; } } else { z[(it->second).city] = (it->second).gain; } int maxCity = 0, traverseCities = 0; long long maxResult = -inf; for(int j=1; j<=n; j++) visited[j] = false; queue<toBfs> q; visited[s] = true; q.push({s, 0}); while(!q.empty()) { auto source = q.front(); q.pop(); for(auto& child : country[source.nrCity]) { if(!visited[child.neigh]) { long long newLoss = source.loss - child.charge; visited[child.neigh] = true; long long eh = (z[child.neigh] + newLoss); if(eh > maxResult) { maxResult = eh; maxCity = child.neigh; } else if(eh == maxResult) { if(maxCity > child.neigh) maxCity = child.neigh; } q.push({child.neigh, newLoss}); traverseCities++; } } } result.push_back(maxCity); s = maxCity; } for(int i=0; i<q; i++) { cout << result[i] << " "; } cout << endl; 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | #include <iostream> #include <vector> #include <unordered_map> #include <queue> const int N = 100000; const long long inf = 9e18; using namespace std; long long z[N]; struct edge { int neigh; long long charge; }; struct change_charge { int a; int b; long long charge; }; struct change_gain { int city; long long gain; }; struct toBfs { int nrCity; long long loss; }; vector<edge> country[N+5]; bool visited[N+5]; vector<int> result; unordered_map<int, change_charge> charges; unordered_map<int, change_gain> profits; int main() { std::ios_base::sync_with_stdio(false); int n, q, a, b, v, type; long long c, d; cin >> n >> q; for(int i=1; i<=n; i++) cin >> z[i]; for(int i=1; i<n; i++) { cin >> a >> b >> c; country[a].push_back({b, c}); country[b].push_back({a, c}); } for(int i=1; i<=q; i++) { cin >> type; if(type==1) { cin >> v >> d; profits[i] = {v, d}; } else { cin >> a >> b >> d; charges[i] = {a, b, d}; } } int s = 1; for(int i=1; i<=q; i++) { auto it = profits.find(i); if(it==profits.end()) { auto it2 = charges.find(i); for(auto& child : country[(it2->second).a]) { if(child.neigh==(it2->second).b) child.charge = (it2->second).charge; } for(auto& child : country[(it2->second).b]) { if(child.neigh==(it2->second).a) child.charge = (it2->second).charge; } } else { z[(it->second).city] = (it->second).gain; } int maxCity = 0, traverseCities = 0; long long maxResult = -inf; for(int j=1; j<=n; j++) visited[j] = false; queue<toBfs> q; visited[s] = true; q.push({s, 0}); while(!q.empty()) { auto source = q.front(); q.pop(); for(auto& child : country[source.nrCity]) { if(!visited[child.neigh]) { long long newLoss = source.loss - child.charge; visited[child.neigh] = true; long long eh = (z[child.neigh] + newLoss); if(eh > maxResult) { maxResult = eh; maxCity = child.neigh; } else if(eh == maxResult) { if(maxCity > child.neigh) maxCity = child.neigh; } q.push({child.neigh, newLoss}); traverseCities++; } } } result.push_back(maxCity); s = maxCity; } for(int i=0; i<q; i++) { cout << result[i] << " "; } cout << endl; return 0; } |