#include <iostream> #include <unordered_map> #include <algorithm> const int N = 100000; typedef long long ll; struct edge { int to; ll cost; inline bool operator() (const edge& left, const edge& right) const { return left.to < right.to; } inline bool operator()(const edge& v, int t) const { return v.to < t; } inline bool operator()(int t1, int t2) const { return t1 < t2; } inline bool operator()(int t, const edge& v) const { return t < v.to; } }; struct node { std::vector<edge> edges; ll profit; ll cost_to_this; }; int n, q; node nodes[N]; int queue[N]; int query_ultra_fast(int source) { int best_node = -1; ll best_cost = std::numeric_limits<ll>::min(); for (int i = 0; i < n; ++i) { nodes[i].cost_to_this = -1; } int queue_index = -1; queue[++queue_index] = source; nodes[source].cost_to_this = 0; while (queue_index >= 0) { source = queue[queue_index--]; for (auto&& edge : nodes[source].edges) { auto& adj_node = nodes[edge.to]; if (adj_node.cost_to_this == -1) { adj_node.cost_to_this = nodes[source].cost_to_this + edge.cost; queue[++queue_index] = edge.to; const ll tmp_profit = adj_node.cost_to_this + adj_node.profit; if (tmp_profit > best_cost) { best_cost = adj_node.cost_to_this + adj_node.profit; best_node = edge.to; } else if (tmp_profit == best_cost && edge.to < best_node) { best_node = edge.to; } } } } return best_node; } int main() { std::ios_base::sync_with_stdio(false); std::cin >> n >> q; for (int i = 0; i < n; ++i) { std::cin >> nodes[i].profit; } for (int i = 0; i < n - 1; ++i) { int from, to; ll cost; std::cin >> from >> to >> cost; --from; --to; cost *= -1; nodes[from].edges.push_back(edge{ to, cost }); nodes[to].edges.push_back(edge{ from, cost }); } for (int i = 0; i < n; i++) { std::sort(nodes[i].edges.begin(), nodes[i].edges.end(), edge()); } int change_type, a, b, new_cost, where_am_i = 0; while (q--) { std::cin >> change_type >> a; if (change_type == 1) { std::cin >> new_cost; nodes[a - 1].profit = new_cost; } else { std::cin >> b >> new_cost; --a; --b; new_cost *= -1; std::lower_bound(nodes[a].edges.begin(), nodes[a].edges.end(), b, edge())->cost = new_cost; std::lower_bound(nodes[b].edges.begin(), nodes[b].edges.end(), a, edge())->cost = new_cost; } where_am_i = query_ultra_fast(where_am_i); std::cout << where_am_i + 1 << ' '; } 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 | #include <iostream> #include <unordered_map> #include <algorithm> const int N = 100000; typedef long long ll; struct edge { int to; ll cost; inline bool operator() (const edge& left, const edge& right) const { return left.to < right.to; } inline bool operator()(const edge& v, int t) const { return v.to < t; } inline bool operator()(int t1, int t2) const { return t1 < t2; } inline bool operator()(int t, const edge& v) const { return t < v.to; } }; struct node { std::vector<edge> edges; ll profit; ll cost_to_this; }; int n, q; node nodes[N]; int queue[N]; int query_ultra_fast(int source) { int best_node = -1; ll best_cost = std::numeric_limits<ll>::min(); for (int i = 0; i < n; ++i) { nodes[i].cost_to_this = -1; } int queue_index = -1; queue[++queue_index] = source; nodes[source].cost_to_this = 0; while (queue_index >= 0) { source = queue[queue_index--]; for (auto&& edge : nodes[source].edges) { auto& adj_node = nodes[edge.to]; if (adj_node.cost_to_this == -1) { adj_node.cost_to_this = nodes[source].cost_to_this + edge.cost; queue[++queue_index] = edge.to; const ll tmp_profit = adj_node.cost_to_this + adj_node.profit; if (tmp_profit > best_cost) { best_cost = adj_node.cost_to_this + adj_node.profit; best_node = edge.to; } else if (tmp_profit == best_cost && edge.to < best_node) { best_node = edge.to; } } } } return best_node; } int main() { std::ios_base::sync_with_stdio(false); std::cin >> n >> q; for (int i = 0; i < n; ++i) { std::cin >> nodes[i].profit; } for (int i = 0; i < n - 1; ++i) { int from, to; ll cost; std::cin >> from >> to >> cost; --from; --to; cost *= -1; nodes[from].edges.push_back(edge{ to, cost }); nodes[to].edges.push_back(edge{ from, cost }); } for (int i = 0; i < n; i++) { std::sort(nodes[i].edges.begin(), nodes[i].edges.end(), edge()); } int change_type, a, b, new_cost, where_am_i = 0; while (q--) { std::cin >> change_type >> a; if (change_type == 1) { std::cin >> new_cost; nodes[a - 1].profit = new_cost; } else { std::cin >> b >> new_cost; --a; --b; new_cost *= -1; std::lower_bound(nodes[a].edges.begin(), nodes[a].edges.end(), b, edge())->cost = new_cost; std::lower_bound(nodes[b].edges.begin(), nodes[b].edges.end(), a, edge())->cost = new_cost; } where_am_i = query_ultra_fast(where_am_i); std::cout << where_am_i + 1 << ' '; } return 0; } |