#include <cstdio> #include <map> struct Node { long long z; std::map<int, long long> e; }; const int MAX = 100100; int n, q; Node m[MAX]; std::pair<long long, int> best(int v, int p = -1, long long d = 0) { std::pair<long long, int> b(1, 1); for (auto &sl : m[v].e) { int s = sl.first; int l = sl.second; if (s != p) { auto bb = best(s, v, d + l); b = std::min(b, bb); } } if (p != -1) { auto bb = std::pair<long long, int>(d - m[v].z, v); b = std::min(b, bb); } return b; } int main(int argc, char *argv[]) { scanf("%i%i", &n, &q); for (int c = 0; c < n; c++) { scanf("%lli", &m[c + 1].z); } for (int c = 1; c < n; c++) { int a, b; long long l; scanf("%i%i%lli", &a, &b, &l); m[a].e[b] = l; m[b].e[a] = l; } int r = 1; for (int d = 0; d < q; d++) { int t; scanf("%i", &t); if (t == 1) { int v; long long z; scanf("%i%lli", &v, &z); m[v].z = z; } if (t == 2) { int a, b; long long l; scanf("%i%i%lli", &a, &b, &l); m[a].e[b] = l; m[b].e[a] = l; } r = best(r).second; printf("%i ", r); } printf("\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 | #include <cstdio> #include <map> struct Node { long long z; std::map<int, long long> e; }; const int MAX = 100100; int n, q; Node m[MAX]; std::pair<long long, int> best(int v, int p = -1, long long d = 0) { std::pair<long long, int> b(1, 1); for (auto &sl : m[v].e) { int s = sl.first; int l = sl.second; if (s != p) { auto bb = best(s, v, d + l); b = std::min(b, bb); } } if (p != -1) { auto bb = std::pair<long long, int>(d - m[v].z, v); b = std::min(b, bb); } return b; } int main(int argc, char *argv[]) { scanf("%i%i", &n, &q); for (int c = 0; c < n; c++) { scanf("%lli", &m[c + 1].z); } for (int c = 1; c < n; c++) { int a, b; long long l; scanf("%i%i%lli", &a, &b, &l); m[a].e[b] = l; m[b].e[a] = l; } int r = 1; for (int d = 0; d < q; d++) { int t; scanf("%i", &t); if (t == 1) { int v; long long z; scanf("%i%lli", &v, &z); m[v].z = z; } if (t == 2) { int a, b; long long l; scanf("%i%i%lli", &a, &b, &l); m[a].e[b] = l; m[b].e[a] = l; } r = best(r).second; printf("%i ", r); } printf("\n"); return 0; } |