#include <iostream> #include <algorithm> using namespace std; typedef long long ll; const int MAX_N = (int) 1e5+1; const ll INF = (ll) 1e18 +1; ll llMax(ll a, ll b){ return (a > b) ? a : b; } class CityVertex{ public: vector<pair<ll, ll>> roads; ll cityIncome; }; class Solution{ public: ll currCityId; CityVertex graph[MAX_N]; Solution(){ currCityId = 1; } void loadGraph(ll n){ ll u,v,cost; for(int i=1; i <=n; ++i){ cin >> cost; graph[i].cityIncome = cost; } for(int i=0; i < n -1; ++i){ cin >> u >> v >> cost; graph[v].roads.emplace_back(u, cost); graph[u].roads.emplace_back(v, cost); } } void updateCityIncome(ll cityId, ll newIncome){ graph[cityId].cityIncome = newIncome; } void updatePathCost(ll cityU, ll cityV, ll newCost){ for(auto & path : graph[cityU].roads){ if(path.first == cityV){ path.second = newCost; break; } } for(auto & path : graph[cityV].roads){ if(path.first == cityU){ path.second = newCost; break; } } } ll findBestCity(){ auto bestCity = DFSVisit(currCityId, -1, 0); currCityId = bestCity.first; return bestCity.first; } pair<ll,ll> DFSVisit(ll u, ll p, ll pathCost){ pair<ll,ll> maxIncome = {-1, -INF}; for(auto road : graph[u].roads){ if(road.first != p){ auto maxUnder = DFSVisit(road.first, u, pathCost + road.second); if(maxUnder.second > maxIncome.second){ maxIncome = maxUnder; }else if(maxUnder.second == maxIncome.second){ maxIncome.first = min(maxIncome.first, maxUnder.second); } } } if(u != currCityId){ if(graph[u].cityIncome - pathCost> maxIncome.second){ maxIncome = {u, graph[u].cityIncome - pathCost}; }else if(graph[u].cityIncome - pathCost== maxIncome.second){ maxIncome.first = min(maxIncome.first, u); } } return maxIncome; } }; int main() { ll n,q; cin >> n >> q; Solution solution; solution.loadGraph(n); //handling queries int type ; ll u,v,cost; for(int i =0; i <q; ++i){ cin >> type; switch(type){ case 1: cin >> u >> cost; solution.updateCityIncome(u, cost); break; case 2: cin >> u >> v >> cost; solution.updatePathCost(u,v,cost); break; } cout << solution.findBestCity() << " "; } 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 130 131 132 133 134 135 136 | #include <iostream> #include <algorithm> using namespace std; typedef long long ll; const int MAX_N = (int) 1e5+1; const ll INF = (ll) 1e18 +1; ll llMax(ll a, ll b){ return (a > b) ? a : b; } class CityVertex{ public: vector<pair<ll, ll>> roads; ll cityIncome; }; class Solution{ public: ll currCityId; CityVertex graph[MAX_N]; Solution(){ currCityId = 1; } void loadGraph(ll n){ ll u,v,cost; for(int i=1; i <=n; ++i){ cin >> cost; graph[i].cityIncome = cost; } for(int i=0; i < n -1; ++i){ cin >> u >> v >> cost; graph[v].roads.emplace_back(u, cost); graph[u].roads.emplace_back(v, cost); } } void updateCityIncome(ll cityId, ll newIncome){ graph[cityId].cityIncome = newIncome; } void updatePathCost(ll cityU, ll cityV, ll newCost){ for(auto & path : graph[cityU].roads){ if(path.first == cityV){ path.second = newCost; break; } } for(auto & path : graph[cityV].roads){ if(path.first == cityU){ path.second = newCost; break; } } } ll findBestCity(){ auto bestCity = DFSVisit(currCityId, -1, 0); currCityId = bestCity.first; return bestCity.first; } pair<ll,ll> DFSVisit(ll u, ll p, ll pathCost){ pair<ll,ll> maxIncome = {-1, -INF}; for(auto road : graph[u].roads){ if(road.first != p){ auto maxUnder = DFSVisit(road.first, u, pathCost + road.second); if(maxUnder.second > maxIncome.second){ maxIncome = maxUnder; }else if(maxUnder.second == maxIncome.second){ maxIncome.first = min(maxIncome.first, maxUnder.second); } } } if(u != currCityId){ if(graph[u].cityIncome - pathCost> maxIncome.second){ maxIncome = {u, graph[u].cityIncome - pathCost}; }else if(graph[u].cityIncome - pathCost== maxIncome.second){ maxIncome.first = min(maxIncome.first, u); } } return maxIncome; } }; int main() { ll n,q; cin >> n >> q; Solution solution; solution.loadGraph(n); //handling queries int type ; ll u,v,cost; for(int i =0; i <q; ++i){ cin >> type; switch(type){ case 1: cin >> u >> cost; solution.updateCityIncome(u, cost); break; case 2: cin >> u >> v >> cost; solution.updatePathCost(u,v,cost); break; } cout << solution.findBestCity() << " "; } return 0; } |