#include <iostream> #include <cstdio> #include <vector> using namespace std; int n, q, a, b, e, x; long long c, MAX; int wynik[100005]; long long tab[100005]; vector < pair <int, long long> > v[100005]; vector < pair <int, long long> > pomoc; vector < pair <int, long long> >::iterator it; int BFS[100005]; long long odleglosc[100005]; int main () { MAX = -1000000000000000001LL; scanf("%d%d", &n, &q); for (int i = 1; i <= n; ++i) scanf("%lld", &tab[i]); for (int i = 1; i < n; ++i) { scanf("%d%d%lld", &a, &b, &c); v[a].push_back(make_pair(b, c)); v[b].push_back(make_pair(a, c)); } wynik[0] = 1; for (int i = 1; i <= q; ++i) { scanf("%d", &x); if (x == 1) { scanf("%d%lld", &a, &c); tab[a] = c; } else { scanf("%d%d%lld", &a, &b, &c); pomoc.clear(); for (it = v[a].begin(); it != v[a].end(); ++it) if (it->first != b) pomoc.push_back(make_pair(it->first, it->second)); v[a].clear(); for (it = pomoc.begin(); it != pomoc.end(); ++it) v[a].push_back(make_pair(it->first, it->second)); v[a].push_back(make_pair(b, c)); pomoc.clear(); for (it = v[b].begin(); it != v[b].end(); ++it) if (it->first != a) pomoc.push_back(make_pair(it->first, it->second)); v[b].clear(); for (it = pomoc.begin(); it != pomoc.end(); ++it) v[b].push_back(make_pair(it->first, it->second)); v[b].push_back(make_pair(a, c)); } for (int j = 1; j <= n; ++j) odleglosc[j] = -1; BFS[0] = wynik[i - 1]; b = 0; e = 1; odleglosc[BFS[0]] = 0; while (b < e) { x = BFS[b]; for (it = v[x].begin(); it != v[x].end(); ++it) if (odleglosc[it->first] == -1) { BFS[e] = it->first; e++; odleglosc[it->first] = odleglosc[x] + it->second; } b++; } MAX = -1000000000000000001LL; for (int j = 1; j <= n; ++j) if (j != wynik[i - 1]) { if (tab[j] - odleglosc[j] > MAX) { MAX = tab[j] - odleglosc[j]; wynik[i] = j; } } printf("%d ", wynik[i]); } 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 71 72 73 74 75 76 77 78 79 80 81 82 | #include <iostream> #include <cstdio> #include <vector> using namespace std; int n, q, a, b, e, x; long long c, MAX; int wynik[100005]; long long tab[100005]; vector < pair <int, long long> > v[100005]; vector < pair <int, long long> > pomoc; vector < pair <int, long long> >::iterator it; int BFS[100005]; long long odleglosc[100005]; int main () { MAX = -1000000000000000001LL; scanf("%d%d", &n, &q); for (int i = 1; i <= n; ++i) scanf("%lld", &tab[i]); for (int i = 1; i < n; ++i) { scanf("%d%d%lld", &a, &b, &c); v[a].push_back(make_pair(b, c)); v[b].push_back(make_pair(a, c)); } wynik[0] = 1; for (int i = 1; i <= q; ++i) { scanf("%d", &x); if (x == 1) { scanf("%d%lld", &a, &c); tab[a] = c; } else { scanf("%d%d%lld", &a, &b, &c); pomoc.clear(); for (it = v[a].begin(); it != v[a].end(); ++it) if (it->first != b) pomoc.push_back(make_pair(it->first, it->second)); v[a].clear(); for (it = pomoc.begin(); it != pomoc.end(); ++it) v[a].push_back(make_pair(it->first, it->second)); v[a].push_back(make_pair(b, c)); pomoc.clear(); for (it = v[b].begin(); it != v[b].end(); ++it) if (it->first != a) pomoc.push_back(make_pair(it->first, it->second)); v[b].clear(); for (it = pomoc.begin(); it != pomoc.end(); ++it) v[b].push_back(make_pair(it->first, it->second)); v[b].push_back(make_pair(a, c)); } for (int j = 1; j <= n; ++j) odleglosc[j] = -1; BFS[0] = wynik[i - 1]; b = 0; e = 1; odleglosc[BFS[0]] = 0; while (b < e) { x = BFS[b]; for (it = v[x].begin(); it != v[x].end(); ++it) if (odleglosc[it->first] == -1) { BFS[e] = it->first; e++; odleglosc[it->first] = odleglosc[x] + it->second; } b++; } MAX = -1000000000000000001LL; for (int j = 1; j <= n; ++j) if (j != wynik[i - 1]) { if (tab[j] - odleglosc[j] > MAX) { MAX = tab[j] - odleglosc[j]; wynik[i] = j; } } printf("%d ", wynik[i]); } printf("\n"); return 0; } |