#include <iostream> #include <vector> #include <stack> #include <map> using namespace std; typedef long long ll; const ll INF = 1000000000000000000; int main() { ios_base::sync_with_stdio(0); int n, q; cin >> n >> q; vector< vector<int> > G(n); vector<ll> wie(n); map< pair<int, int>, ll > kraw; for (int i = 0; i < n; ++i) cin >> wie[i]; for (int i = 0; i < n - 1; ++i) { int a, b; ll c; cin >> a >> b >> c; --a; --b; G[a].push_back(b); G[b].push_back(a); kraw[make_pair(a, b)] = c; kraw[make_pair(b, a)] = c; } int now = 0; for (int i = 0; i < q; ++i) { int op; cin >> op; if (op == 1) { int v; ll d; cin >> v >> d; --v; wie[v] = d; } else { int a, b; ll d; cin >> a >> b >> d; --a; --b; kraw[make_pair(a, b)] = d; kraw[make_pair(b, a)] = d; } stack<int> S; vector<bool> odw(n, false); vector<ll> odl(n, 0); S.push(now); odw[now] = true; while (!S.empty()) { int akt = S.top(); S.pop(); for (int j = 0; j < G[akt].size(); ++j) { if (!odw[G[akt][j]]) { odw[G[akt][j]] = true; odl[G[akt][j]] = odl[akt] - kraw[make_pair(akt, G[akt][j])] + wie[G[akt][j]]; S.push(G[akt][j]); } } } ll odp = -1, zysk = -INF; for (int j = 0; j < n; ++j) { if (odl[j] > zysk) { odp = j; zysk = odl[j]; } } now = odp; cout << now + 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 | #include <iostream> #include <vector> #include <stack> #include <map> using namespace std; typedef long long ll; const ll INF = 1000000000000000000; int main() { ios_base::sync_with_stdio(0); int n, q; cin >> n >> q; vector< vector<int> > G(n); vector<ll> wie(n); map< pair<int, int>, ll > kraw; for (int i = 0; i < n; ++i) cin >> wie[i]; for (int i = 0; i < n - 1; ++i) { int a, b; ll c; cin >> a >> b >> c; --a; --b; G[a].push_back(b); G[b].push_back(a); kraw[make_pair(a, b)] = c; kraw[make_pair(b, a)] = c; } int now = 0; for (int i = 0; i < q; ++i) { int op; cin >> op; if (op == 1) { int v; ll d; cin >> v >> d; --v; wie[v] = d; } else { int a, b; ll d; cin >> a >> b >> d; --a; --b; kraw[make_pair(a, b)] = d; kraw[make_pair(b, a)] = d; } stack<int> S; vector<bool> odw(n, false); vector<ll> odl(n, 0); S.push(now); odw[now] = true; while (!S.empty()) { int akt = S.top(); S.pop(); for (int j = 0; j < G[akt].size(); ++j) { if (!odw[G[akt][j]]) { odw[G[akt][j]] = true; odl[G[akt][j]] = odl[akt] - kraw[make_pair(akt, G[akt][j])] + wie[G[akt][j]]; S.push(G[akt][j]); } } } ll odp = -1, zysk = -INF; for (int j = 0; j < n; ++j) { if (odl[j] > zysk) { odp = j; zysk = odl[j]; } } now = odp; cout << now + 1 << " "; } return 0; } |