#include <cstdio> #include <vector> #include <set> #include <queue> #include <algorithm> #include <climits> using namespace std; typedef long long ll_t; struct node_t { int parent; ll_t profit; ll_t to_parent_cost; ll_t subtree_maximum_profit; // id of node for which profit of traversing to is equal to subtree_maximum_profit int subtree_optimal_node; set<int> children; // initially stores all edge information - it will be used to build the rooted tree and populate children set<pair<int, ll_t> > neighbors; int level; // level in the rooted tree node_t(ll_t profit, int id) { this->profit = profit; this->subtree_maximum_profit = profit; this->subtree_optimal_node = id; this->parent = -1; // -1 means the parent is currently unknown, 0 means the node is the root this->level = LONG_MAX; } }; // G[i] contains node of id = i vector<node_t> G; ll_t max_profit; int min_optimal_node_id; bool cmp_desc_by_level(int id1, int id2) { return G[id1].level > G[id2].level; } void build_rooted_tree() { queue<int> Q; G[1].parent = 0; // make node 1 the root G[1].level = 0; Q.push(1); while (!Q.empty()) { int parent = Q.front(); Q.pop(); for (set<pair<int, ll_t> >::iterator it = G[parent].neighbors.begin(); it != G[parent].neighbors.end(); it++) { if (G[it->first].parent == -1) { // parent unknown int child = it->first; G[parent].children.insert(child); G[child].parent = parent; G[child].level = G[parent].level + 1; G[child].to_parent_cost = it->second; Q.push(child); } } } } void calculate_maximum_profit(int id) { G[id].subtree_maximum_profit = G[id].profit; G[id].subtree_optimal_node = id; for (set<int>::iterator it = G[id].children.begin(); it != G[id].children.end(); it++) { int child_id = *it; if (G[child_id].subtree_maximum_profit - G[child_id].to_parent_cost > G[id].subtree_maximum_profit || (G[child_id].subtree_maximum_profit - G[child_id].to_parent_cost == G[id].subtree_maximum_profit && G[child_id].subtree_optimal_node < G[id].subtree_optimal_node)) { G[id].subtree_maximum_profit = G[child_id].subtree_maximum_profit - G[child_id].to_parent_cost; G[id].subtree_optimal_node = G[child_id].subtree_optimal_node; } } } void update_node_weight(int id, ll_t new_weight) { G[id].profit = new_weight; if (new_weight > G[id].subtree_maximum_profit) { G[id].subtree_maximum_profit = new_weight; G[id].subtree_optimal_node = id; } else { G[id].subtree_maximum_profit = new_weight; G[id].subtree_optimal_node = id; for (set<int>::iterator it = G[id].children.begin(); it != G[id].children.end(); it++) { int child_id = *it; if (G[child_id].subtree_maximum_profit - G[child_id].to_parent_cost > G[id].subtree_maximum_profit || (G[child_id].subtree_maximum_profit - G[child_id].to_parent_cost == G[id].subtree_maximum_profit && G[child_id].subtree_optimal_node < G[id].subtree_optimal_node)) { G[id].subtree_maximum_profit = G[child_id].subtree_maximum_profit - G[child_id].to_parent_cost; G[id].subtree_optimal_node = G[child_id].subtree_optimal_node; } } } bool profit_updated = true; int child_id = id; id = G[id].parent; while (id != 0 && profit_updated) { ll_t candidate_profit = G[child_id].subtree_maximum_profit - G[child_id].to_parent_cost; if (candidate_profit > G[id].subtree_maximum_profit || (candidate_profit == G[id].subtree_maximum_profit && G[child_id].subtree_optimal_node < G[id].subtree_optimal_node)) { G[id].subtree_maximum_profit = G[child_id].subtree_maximum_profit - G[child_id].to_parent_cost; G[id].subtree_optimal_node = G[child_id].subtree_optimal_node; } else { profit_updated = false; } child_id = id; id = G[id].parent; } } void update_edge_weight(int id, int child_id, ll_t new_weight) { G[child_id].to_parent_cost = new_weight; while (id != 0) { calculate_maximum_profit(id); child_id = id; id = G[id].parent; } } void candidate(ll_t profit, int id) { if (profit > max_profit || (profit == max_profit && id < min_optimal_node_id)) { max_profit = profit; min_optimal_node_id = id; } } int next_optimal_node(int starting_node) { max_profit = LLONG_MIN; min_optimal_node_id = LONG_MAX; candidate(G[starting_node].subtree_maximum_profit, G[starting_node].subtree_optimal_node); int child_id = starting_node; int id = G[starting_node].parent; ll_t path_cost = 0LL; while (id != 0) { path_cost += G[child_id].to_parent_cost; candidate(G[id].subtree_maximum_profit - path_cost, G[id].subtree_optimal_node); child_id = id; id = G[id].parent; } if (min_optimal_node_id == starting_node) { // search again, this time ignoring starting_node max_profit = LLONG_MIN; min_optimal_node_id = LONG_MAX; for (set<int>::iterator it = G[starting_node].children.begin(); it != G[starting_node].children.end(); it++) { child_id = *it; candidate(G[child_id].subtree_maximum_profit - G[child_id].to_parent_cost, G[child_id].subtree_optimal_node); } child_id = starting_node; id = G[starting_node].parent; path_cost = 0LL; while (id != 0) { path_cost += G[child_id].to_parent_cost; candidate(G[id].profit - path_cost, id); for (set<int>::iterator it = G[id].children.begin(); it != G[id].children.end(); it++) { if (*it != child_id) { int other_child_id = *it; candidate(G[other_child_id].subtree_maximum_profit - path_cost - G[other_child_id].to_parent_cost, G[other_child_id].subtree_optimal_node); } } child_id = id; id = G[id].parent; } } return min_optimal_node_id; } int main() { int n, q; scanf("%d%d", &n, &q); G.push_back(node_t(0, 0)); // dummy node of id = 0 for (int i = 1; i <= n; i++) { ll_t z; scanf("%lld", &z); G.push_back(node_t(z, i)); } for (int i = 0; i < n - 1; i++) { int a, b; ll_t c; scanf("%d%d%lld", &a, &b, &c); G[a].neighbors.insert(make_pair(b, c)); G[b].neighbors.insert(make_pair(a, c)); } build_rooted_tree(); vector<int> node_ids; for (int id = 1; id <= n; id++) { node_ids.push_back(id); } sort(node_ids.begin(), node_ids.end(), cmp_desc_by_level); for (int i = 0; i < n; i++) { calculate_maximum_profit(node_ids[i]); } int current_node = 1; for (int i = 0; i < q; i++) { char type; int a, b, v; ll_t d; scanf("%d", &type); if (type == 1) { scanf("%d%lld", &v, &d); update_node_weight(v, d); } else { scanf("%d%d%lld", &a, &b, &d); // make sure a is parent of b if (G[b].parent != a) { swap(a, b); } update_edge_weight(a, b, d); } current_node = next_optimal_node(current_node); printf("%d ", current_node); } putchar('\n'); 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | #include <cstdio> #include <vector> #include <set> #include <queue> #include <algorithm> #include <climits> using namespace std; typedef long long ll_t; struct node_t { int parent; ll_t profit; ll_t to_parent_cost; ll_t subtree_maximum_profit; // id of node for which profit of traversing to is equal to subtree_maximum_profit int subtree_optimal_node; set<int> children; // initially stores all edge information - it will be used to build the rooted tree and populate children set<pair<int, ll_t> > neighbors; int level; // level in the rooted tree node_t(ll_t profit, int id) { this->profit = profit; this->subtree_maximum_profit = profit; this->subtree_optimal_node = id; this->parent = -1; // -1 means the parent is currently unknown, 0 means the node is the root this->level = LONG_MAX; } }; // G[i] contains node of id = i vector<node_t> G; ll_t max_profit; int min_optimal_node_id; bool cmp_desc_by_level(int id1, int id2) { return G[id1].level > G[id2].level; } void build_rooted_tree() { queue<int> Q; G[1].parent = 0; // make node 1 the root G[1].level = 0; Q.push(1); while (!Q.empty()) { int parent = Q.front(); Q.pop(); for (set<pair<int, ll_t> >::iterator it = G[parent].neighbors.begin(); it != G[parent].neighbors.end(); it++) { if (G[it->first].parent == -1) { // parent unknown int child = it->first; G[parent].children.insert(child); G[child].parent = parent; G[child].level = G[parent].level + 1; G[child].to_parent_cost = it->second; Q.push(child); } } } } void calculate_maximum_profit(int id) { G[id].subtree_maximum_profit = G[id].profit; G[id].subtree_optimal_node = id; for (set<int>::iterator it = G[id].children.begin(); it != G[id].children.end(); it++) { int child_id = *it; if (G[child_id].subtree_maximum_profit - G[child_id].to_parent_cost > G[id].subtree_maximum_profit || (G[child_id].subtree_maximum_profit - G[child_id].to_parent_cost == G[id].subtree_maximum_profit && G[child_id].subtree_optimal_node < G[id].subtree_optimal_node)) { G[id].subtree_maximum_profit = G[child_id].subtree_maximum_profit - G[child_id].to_parent_cost; G[id].subtree_optimal_node = G[child_id].subtree_optimal_node; } } } void update_node_weight(int id, ll_t new_weight) { G[id].profit = new_weight; if (new_weight > G[id].subtree_maximum_profit) { G[id].subtree_maximum_profit = new_weight; G[id].subtree_optimal_node = id; } else { G[id].subtree_maximum_profit = new_weight; G[id].subtree_optimal_node = id; for (set<int>::iterator it = G[id].children.begin(); it != G[id].children.end(); it++) { int child_id = *it; if (G[child_id].subtree_maximum_profit - G[child_id].to_parent_cost > G[id].subtree_maximum_profit || (G[child_id].subtree_maximum_profit - G[child_id].to_parent_cost == G[id].subtree_maximum_profit && G[child_id].subtree_optimal_node < G[id].subtree_optimal_node)) { G[id].subtree_maximum_profit = G[child_id].subtree_maximum_profit - G[child_id].to_parent_cost; G[id].subtree_optimal_node = G[child_id].subtree_optimal_node; } } } bool profit_updated = true; int child_id = id; id = G[id].parent; while (id != 0 && profit_updated) { ll_t candidate_profit = G[child_id].subtree_maximum_profit - G[child_id].to_parent_cost; if (candidate_profit > G[id].subtree_maximum_profit || (candidate_profit == G[id].subtree_maximum_profit && G[child_id].subtree_optimal_node < G[id].subtree_optimal_node)) { G[id].subtree_maximum_profit = G[child_id].subtree_maximum_profit - G[child_id].to_parent_cost; G[id].subtree_optimal_node = G[child_id].subtree_optimal_node; } else { profit_updated = false; } child_id = id; id = G[id].parent; } } void update_edge_weight(int id, int child_id, ll_t new_weight) { G[child_id].to_parent_cost = new_weight; while (id != 0) { calculate_maximum_profit(id); child_id = id; id = G[id].parent; } } void candidate(ll_t profit, int id) { if (profit > max_profit || (profit == max_profit && id < min_optimal_node_id)) { max_profit = profit; min_optimal_node_id = id; } } int next_optimal_node(int starting_node) { max_profit = LLONG_MIN; min_optimal_node_id = LONG_MAX; candidate(G[starting_node].subtree_maximum_profit, G[starting_node].subtree_optimal_node); int child_id = starting_node; int id = G[starting_node].parent; ll_t path_cost = 0LL; while (id != 0) { path_cost += G[child_id].to_parent_cost; candidate(G[id].subtree_maximum_profit - path_cost, G[id].subtree_optimal_node); child_id = id; id = G[id].parent; } if (min_optimal_node_id == starting_node) { // search again, this time ignoring starting_node max_profit = LLONG_MIN; min_optimal_node_id = LONG_MAX; for (set<int>::iterator it = G[starting_node].children.begin(); it != G[starting_node].children.end(); it++) { child_id = *it; candidate(G[child_id].subtree_maximum_profit - G[child_id].to_parent_cost, G[child_id].subtree_optimal_node); } child_id = starting_node; id = G[starting_node].parent; path_cost = 0LL; while (id != 0) { path_cost += G[child_id].to_parent_cost; candidate(G[id].profit - path_cost, id); for (set<int>::iterator it = G[id].children.begin(); it != G[id].children.end(); it++) { if (*it != child_id) { int other_child_id = *it; candidate(G[other_child_id].subtree_maximum_profit - path_cost - G[other_child_id].to_parent_cost, G[other_child_id].subtree_optimal_node); } } child_id = id; id = G[id].parent; } } return min_optimal_node_id; } int main() { int n, q; scanf("%d%d", &n, &q); G.push_back(node_t(0, 0)); // dummy node of id = 0 for (int i = 1; i <= n; i++) { ll_t z; scanf("%lld", &z); G.push_back(node_t(z, i)); } for (int i = 0; i < n - 1; i++) { int a, b; ll_t c; scanf("%d%d%lld", &a, &b, &c); G[a].neighbors.insert(make_pair(b, c)); G[b].neighbors.insert(make_pair(a, c)); } build_rooted_tree(); vector<int> node_ids; for (int id = 1; id <= n; id++) { node_ids.push_back(id); } sort(node_ids.begin(), node_ids.end(), cmp_desc_by_level); for (int i = 0; i < n; i++) { calculate_maximum_profit(node_ids[i]); } int current_node = 1; for (int i = 0; i < q; i++) { char type; int a, b, v; ll_t d; scanf("%d", &type); if (type == 1) { scanf("%d%lld", &v, &d); update_node_weight(v, d); } else { scanf("%d%d%lld", &a, &b, &d); // make sure a is parent of b if (G[b].parent != a) { swap(a, b); } update_edge_weight(a, b, d); } current_node = next_optimal_node(current_node); printf("%d ", current_node); } putchar('\n'); return 0; } |