#include <iostream> #include <vector> #include <climits> using namespace std; const long long BASE = 8000000000000000000LL; struct big_int { long long first; long long second; big_int(long long i, long long s = 0) { first = i; second = s; } big_int plus(long long a) { big_int result = *this; if (BASE - a <= result.first) { result.first -= BASE; result.first += a; result.second += 1; } else { result.first += a; } return result; } big_int minus(long long a) { big_int result = *this; if (result.first - a < 0) { result.first += BASE; result.first -= a; result.second -= 1; } else { result.first -= a; } return result; } int is_bigger_than(const big_int &a) { if (this->second > a.second) { return 1; } else if (this->second == a.second) { if (this->first > a.first) { return 1; } else if (this->first == a.first) { return 0; } else { return -1; } } else { return -1; } } }; struct edge { long long cost; int dest; }; struct node { long long income; vector<edge> edges; void change_edge(int dest, long long cost) { for (edge &e : edges) { if (e.dest == dest) { e.cost = cost; return; } } } }; int n, q; vector<node> nodes(100001); int current = 1; int max_city; big_int max_total_income(0, -1); vector<bool> visited(100001); void dfs(int a, big_int income) { visited[a] = true; big_int this_income = income.plus(nodes[a].income); int comp_result = this_income.is_bigger_than(max_total_income); if (comp_result > 0 || (comp_result == 0 && a < max_city)) { max_city = a; max_total_income = this_income; } for (const edge &e : nodes[a].edges) { if (!visited[e.dest]) { dfs(e.dest, income.minus(e.cost)); } } } int next_city() { max_city = 100001; max_total_income = big_int(0, -1); for (int i = 1; i <= n; i++) { visited[i] = false; } visited[current] = true; big_int income(0); for (const edge &e : nodes[current].edges) { dfs(e.dest, income.minus(e.cost)); } return max_city; } int main() { cin >> n; cin >> q; for (int i = 1; i <= n; i++) { cin >> nodes[i].income; } for (int i = 0; i < n - 1; i++) { int a, b; long long cost; cin >> a >> b >> cost; edge first; first.dest = b; first.cost = cost; edge second; second.dest = a; second.cost = cost; nodes[a].edges.push_back(first); nodes[b].edges.push_back(second); } for (int i = 0; i < q; i++) { int t; cin >> t; if (t == 1) { int v; long long d; cin >> v >> d; nodes[v].income = d; } else { int a, b; long long d; cin >> a >> b >> d; nodes[a].change_edge(b, d); nodes[b].change_edge(a, d); } current = next_city(); cout << current << " "; } 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 | #include <iostream> #include <vector> #include <climits> using namespace std; const long long BASE = 8000000000000000000LL; struct big_int { long long first; long long second; big_int(long long i, long long s = 0) { first = i; second = s; } big_int plus(long long a) { big_int result = *this; if (BASE - a <= result.first) { result.first -= BASE; result.first += a; result.second += 1; } else { result.first += a; } return result; } big_int minus(long long a) { big_int result = *this; if (result.first - a < 0) { result.first += BASE; result.first -= a; result.second -= 1; } else { result.first -= a; } return result; } int is_bigger_than(const big_int &a) { if (this->second > a.second) { return 1; } else if (this->second == a.second) { if (this->first > a.first) { return 1; } else if (this->first == a.first) { return 0; } else { return -1; } } else { return -1; } } }; struct edge { long long cost; int dest; }; struct node { long long income; vector<edge> edges; void change_edge(int dest, long long cost) { for (edge &e : edges) { if (e.dest == dest) { e.cost = cost; return; } } } }; int n, q; vector<node> nodes(100001); int current = 1; int max_city; big_int max_total_income(0, -1); vector<bool> visited(100001); void dfs(int a, big_int income) { visited[a] = true; big_int this_income = income.plus(nodes[a].income); int comp_result = this_income.is_bigger_than(max_total_income); if (comp_result > 0 || (comp_result == 0 && a < max_city)) { max_city = a; max_total_income = this_income; } for (const edge &e : nodes[a].edges) { if (!visited[e.dest]) { dfs(e.dest, income.minus(e.cost)); } } } int next_city() { max_city = 100001; max_total_income = big_int(0, -1); for (int i = 1; i <= n; i++) { visited[i] = false; } visited[current] = true; big_int income(0); for (const edge &e : nodes[current].edges) { dfs(e.dest, income.minus(e.cost)); } return max_city; } int main() { cin >> n; cin >> q; for (int i = 1; i <= n; i++) { cin >> nodes[i].income; } for (int i = 0; i < n - 1; i++) { int a, b; long long cost; cin >> a >> b >> cost; edge first; first.dest = b; first.cost = cost; edge second; second.dest = a; second.cost = cost; nodes[a].edges.push_back(first); nodes[b].edges.push_back(second); } for (int i = 0; i < q; i++) { int t; cin >> t; if (t == 1) { int v; long long d; cin >> v >> d; nodes[v].income = d; } else { int a, b; long long d; cin >> a >> b >> d; nodes[a].change_edge(b, d); nodes[b].change_edge(a, d); } current = next_city(); cout << current << " "; } return 0; } |