#include <iostream> #include <algorithm> #include <vector> using namespace std; const long long MIN = -1e18; bool visited[100007]; long long selling[100007]; vector<pair<int, long long>> edges[100007]; void changeRoad(int a, int b, long long &d) { int i = 0; while (edges[a][i].first != b) ++i; edges[a][i].second = d; } pair<int, long long> findTheBestCity(int city, long long roads) { visited[city] = true; pair<int, long long> ret = make_pair(city, selling[city] - roads); for (int j = 0; j < (int)edges[city].size(); ++j) { pair<int, long long> next = edges[city][j]; if (!visited[next.first]) { pair<int, long long> x = findTheBestCity(next.first, roads + next.second); if (ret.second < x.second || (ret.second == x.second && ret.first > x.first)) ret = x; } } return ret; } int main() { int n,q; cin >> n >> q; for (int i = 1; i <= n; ++i) cin >> selling[i]; for (int i = 0; i < n - 1; ++i) { int a, b; long long c; cin >> a >> b >> c; edges[a].emplace_back(b, c); edges[b].emplace_back(a, c); } int prev_city = 1; for (int i = 0; i < q; ++i) { int kind; cin >> kind; if (kind == 1) { int v; long long d; cin >> v >> d; selling[v] = d; } else { int a, b; long long d; cin >> a >> b >> d; changeRoad(a, b, d); changeRoad(b, a, d); } for(int j = 1; j <= n; j++) visited[j] = false; visited[prev_city] = true; pair<int, long long> ret = make_pair(-1, MIN); for(int j = 0; j < (int)edges[prev_city].size(); ++j) { pair<int, long long> next = edges[prev_city][j]; if (!visited[next.first]) { pair<int, long long> x = findTheBestCity(next.first, next.second); if (ret.second < x.second) ret = x; } } cout << ret.first << " "; prev_city = ret.first; } 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 | #include <iostream> #include <algorithm> #include <vector> using namespace std; const long long MIN = -1e18; bool visited[100007]; long long selling[100007]; vector<pair<int, long long>> edges[100007]; void changeRoad(int a, int b, long long &d) { int i = 0; while (edges[a][i].first != b) ++i; edges[a][i].second = d; } pair<int, long long> findTheBestCity(int city, long long roads) { visited[city] = true; pair<int, long long> ret = make_pair(city, selling[city] - roads); for (int j = 0; j < (int)edges[city].size(); ++j) { pair<int, long long> next = edges[city][j]; if (!visited[next.first]) { pair<int, long long> x = findTheBestCity(next.first, roads + next.second); if (ret.second < x.second || (ret.second == x.second && ret.first > x.first)) ret = x; } } return ret; } int main() { int n,q; cin >> n >> q; for (int i = 1; i <= n; ++i) cin >> selling[i]; for (int i = 0; i < n - 1; ++i) { int a, b; long long c; cin >> a >> b >> c; edges[a].emplace_back(b, c); edges[b].emplace_back(a, c); } int prev_city = 1; for (int i = 0; i < q; ++i) { int kind; cin >> kind; if (kind == 1) { int v; long long d; cin >> v >> d; selling[v] = d; } else { int a, b; long long d; cin >> a >> b >> d; changeRoad(a, b, d); changeRoad(b, a, d); } for(int j = 1; j <= n; j++) visited[j] = false; visited[prev_city] = true; pair<int, long long> ret = make_pair(-1, MIN); for(int j = 0; j < (int)edges[prev_city].size(); ++j) { pair<int, long long> next = edges[prev_city][j]; if (!visited[next.first]) { pair<int, long long> x = findTheBestCity(next.first, next.second); if (ret.second < x.second) ret = x; } } cout << ret.first << " "; prev_city = ret.first; } return 0; } |