#define _CRT_SECURE_NO_WARNINGS 1 #define _WINSOCK_DEPRECATED_NO_WARNINGS 1 #include <cstdio> #include <vector> #include <stack> #include <map> #include <algorithm> using namespace std; struct City { long long int profit; vector<int> roads; int toVisit; long long int road_cost; }; struct Road { int cost; int a; int b; }; long long int td; int n,q,ta,tb,tc; City c[1000001]; Road r[1000001]; int w[1000001]; map<long long int, int> mr; int s[2000001]; int si; int main() { scanf("%d%d", &n, &q); for (int i = 1; i <= n; ++i) { scanf("%lld", &c[i].profit); } for (int i = 0; i < n - 1; ++i) { scanf("%d%d%d", &ta, &tb, &tc); r[i].cost = tc; r[i].a = ta; r[i].b = tb; c[ta].roads.push_back(i); c[tb].roads.push_back(i); long long int la = ta + tb * 1000000; long long int lb = tb + ta * 1000000; mr[la] = i; mr[lb] = i; } int current = 1; for (int i = 0; i < q; ++i) { scanf("%d", &ta); if (ta == 1) { scanf("%d%lld", &tb, &td); c[tb].profit = td; } if (ta == 2) { scanf("%d%d%d", &ta, &tb, &tc); long long int la = ta + tb * 1000000; int ri = mr[la]; r[ri].cost = tc; } for (int j = 1; j <= n; ++j) { c[j].toVisit = -1; c[j].road_cost = 0; } s[0] = 0; s[1] = current; si = 2; while (si > 1) { int ci = s[si - 1]; int pi = s[si - 2]; int j = c[ci].toVisit + 1; for (; j < c[ci].roads.size(); ++j) { int ri = c[ci].roads[j]; int nci = r[ri].a == ci ? r[ri].b : r[ri].a; if (nci != pi) { // wizytujemy nowe miasto s[si] = nci; si++; c[nci].road_cost = c[ci].road_cost + r[ri].cost; break; } } c[ci].toVisit = j; if (j >= c[ci].roads.size()) { si--; } } // wybieramy maksymalny zysk long long int max_gain = -9223372036854775800; int max_city = -1; for (int j = 1; j <= n; ++j) { if (j != current) { long long int city_gain = c[j].profit - c[j].road_cost; if (city_gain > max_gain) { max_city = j; max_gain = city_gain; } } } current = max_city; w[i] = max_city; } for (int i = 0; i < q; ++i) { printf("%d ", w[i]); } 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 | #define _CRT_SECURE_NO_WARNINGS 1 #define _WINSOCK_DEPRECATED_NO_WARNINGS 1 #include <cstdio> #include <vector> #include <stack> #include <map> #include <algorithm> using namespace std; struct City { long long int profit; vector<int> roads; int toVisit; long long int road_cost; }; struct Road { int cost; int a; int b; }; long long int td; int n,q,ta,tb,tc; City c[1000001]; Road r[1000001]; int w[1000001]; map<long long int, int> mr; int s[2000001]; int si; int main() { scanf("%d%d", &n, &q); for (int i = 1; i <= n; ++i) { scanf("%lld", &c[i].profit); } for (int i = 0; i < n - 1; ++i) { scanf("%d%d%d", &ta, &tb, &tc); r[i].cost = tc; r[i].a = ta; r[i].b = tb; c[ta].roads.push_back(i); c[tb].roads.push_back(i); long long int la = ta + tb * 1000000; long long int lb = tb + ta * 1000000; mr[la] = i; mr[lb] = i; } int current = 1; for (int i = 0; i < q; ++i) { scanf("%d", &ta); if (ta == 1) { scanf("%d%lld", &tb, &td); c[tb].profit = td; } if (ta == 2) { scanf("%d%d%d", &ta, &tb, &tc); long long int la = ta + tb * 1000000; int ri = mr[la]; r[ri].cost = tc; } for (int j = 1; j <= n; ++j) { c[j].toVisit = -1; c[j].road_cost = 0; } s[0] = 0; s[1] = current; si = 2; while (si > 1) { int ci = s[si - 1]; int pi = s[si - 2]; int j = c[ci].toVisit + 1; for (; j < c[ci].roads.size(); ++j) { int ri = c[ci].roads[j]; int nci = r[ri].a == ci ? r[ri].b : r[ri].a; if (nci != pi) { // wizytujemy nowe miasto s[si] = nci; si++; c[nci].road_cost = c[ci].road_cost + r[ri].cost; break; } } c[ci].toVisit = j; if (j >= c[ci].roads.size()) { si--; } } // wybieramy maksymalny zysk long long int max_gain = -9223372036854775800; int max_city = -1; for (int j = 1; j <= n; ++j) { if (j != current) { long long int city_gain = c[j].profit - c[j].road_cost; if (city_gain > max_gain) { max_city = j; max_gain = city_gain; } } } current = max_city; w[i] = max_city; } for (int i = 0; i < q; ++i) { printf("%d ", w[i]); } return 0; } |