#include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto operator<<(auto&o,auto x)->decltype(x.end(),o); auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<", "<<get<3>(t)<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif int n, m, q; vector<set<pair<int, int>>> graph; map<pair<int, int>, int> edge_cost; vector<tuple<int, int, LL, int>> queries; namespace brute { int time = 0; int k; vector<int> color; vector<int> visited; void dfs(int v, LL z) { visited[v] = time; color[v] = k; for (auto [b, d] : graph[v]) if (visited[b] != time and d <= z) dfs(b, z - d); } void solve() { color.resize(n); visited.resize(n); for (auto [type, a, b, c] : queries) { if (type == 1) { graph[a].emplace(b, c); graph[b].emplace(a, c); edge_cost[pair(a, b)] = c; edge_cost[pair(b, a)] = c; } else if (type == 2) { const int d = edge_cost[pair(a, b)]; graph[a].erase(pair(b, d)); graph[b].erase(pair(a, d)); } else if (type == 3) { ++time; k = c; dfs(a, b); } else { assert(type == 4); cout << color[a] << '\n'; } } } } // brute using Info = pair<int, int>; // {time, color} namespace centro { struct Tree { int sz = 1; vector<Info> tree; Tree() {} Tree(int N) { while (sz < N) sz *= 2; tree.resize(2 * sz); } int l, r; Info val; void upd(int w, int a, int b) { if (a > r or b < l) return; if (a >= l and b <= r) { tree[w] = max(tree[w], val); return; } const int mid = (a + b) / 2; upd(w * 2, a, mid); upd(w * 2 + 1, mid + 1, b); } void update(int _l, int _r, Info _val) { l = _l; r = _r; val = _val; upd(1, 0, sz - 1); } Info query(int x) { Info ret = {0, 0}; x += sz; while (x) { ret = max(ret, tree[x]); x /= 2; } return ret; } }; vector<Tree> tree; vector<vector<LL>> dists; vector<vector<tuple<LL, int, int>>> centros; // {dist, centro, id_in_cetro} struct CentroDecomp { const vector<set<pair<int, int>>> &graph; // tu vector<int> par, podsz, odwi; int odwi_cnt = 1; const int INF = int(1e9); int root; void refresh() { ++odwi_cnt; } void visit(int v) { odwi[v] = max(odwi[v], odwi_cnt); } bool is_vis(int v) { return odwi[v] >= odwi_cnt; } void dfs_podsz(int v) { visit(v); podsz[v] = 1; for (auto [u, _] : graph[v]) // tu if (!is_vis(u)) { dfs_podsz(u); podsz[v] += podsz[u]; } } int centro(int v) { refresh(); dfs_podsz(v); int sz = podsz[v] / 2; refresh(); while (true) { visit(v); for (auto [u, _] : graph[v]) // tu if (!is_vis(u) && podsz[u] > sz) { v = u; break; } if (is_vis(v)) return v; } } void decomp(int v) { refresh(); // Tu kod. Centroid to v, ktory jest juz dozywotnie odwiedzony. vector<pair<LL, int>> children; function<void(int, LL)> dfs = [&](int a, LL len) { children.emplace_back(len, a); visit(a); for (auto [b, c] : graph[a]) { if (not is_vis(b)) { dfs(b, len + c); } } }; dfs(v, 0); sort(children.begin(), children.end()); const int s = ssize(children); tree[v] = Tree(s); REP(i, s) { auto [len, a] = children[i]; centros[a].emplace_back(len, v, i); dists[v].emplace_back(len); } // Koniec kodu. refresh(); for(auto [u, _] : graph[v]) // tu if (!is_vis(u)) { u = centro(u); par[u] = v; odwi[u] = INF; // Opcjonalnie tutaj przekazujemy info synowi w drzewie CD. decomp(u); } } CentroDecomp(int N, vector<set<pair<int, int>>> &grph) // tu : graph(grph), par(N, -1), podsz(N), odwi(N) { root = centro(0); odwi[root] = INF; decomp(root); } }; void solve() { tree.resize(n); dists.resize(n); centros.resize(n); CentroDecomp cd(n, graph); for (auto& v : centros) sort(v.begin(), v.end()); int time = 0; for (auto [type, a, b, c] : queries) { ++time; if (type == 3) { for (auto [dist, centro, _] : centros[a]) { if (dist > b) break; auto it = upper_bound(dists[centro].begin(), dists[centro].end(), b - dist); tree[centro].update(0, int(it - dists[centro].begin()) - 1, {time, c}); } } else { assert(type == 4); Info ans = {0, 0}; for (auto [_, centro, id_in_cetro] : centros[a]) { ans = max(ans, tree[centro].query(id_in_cetro)); } cout << ans.second << '\n'; } } } } // centro namespace FU { struct FindUnion { vector<Info> color; vector<int> size, par, par_time; vector<pair<int, int>> history; // {a, b} - a connected to b FindUnion(int N) : color(N, Info{-1, 0}), size(N, 1), par(N, -1), par_time(N, -1) {} int find(int a) { return par[a] == -1 ? a : find(par[a]); } void join(int a, int b, int time) { a = find(a); b = find(b); if (size[a] > size[b]) swap(a, b); par[a] = b; par_time[a] = time; size[b] += size[a]; history.emplace_back(a, b); } void rollback() { assert(not history.empty()); auto [a, b] = history.back(); history.pop_back(); par[a] = -1; size[b] -= size[a]; if (color[b].first >= par_time[a]) color[a] = max(color[a], color[b]); } void change_color(int a, Info val) { a = find(a); color[a] = max(color[a], val); } Info get_color(int a) { auto ret = color[a]; int time = -1; while (par[a] != -1) { time = max(time, par_time[a]); a = par[a]; if (color[a].first >= time) ret = max(ret, color[a]); } return ret; } }; int sz = 1; vector<vector<tuple<int, int, int>>> updates; // {a, b, time} void update(int w, int wa, int wb, int l, int r, int a, int b) { if (wa > r or wb < l) return; if (wa >= l and wb <= r) { updates[w].emplace_back(a, b, wa); return; } const int mid = (wa + wb) / 2; update(w * 2, wa, mid, l, r, a, b); update(w * 2 + 1, mid + 1, wb, l, r, a, b); } void solve() { FindUnion fu(n); while (sz < q + 2) sz *= 2; updates.resize(2 * sz); map<pair<int, int>, int> appearance; REP(a, n) for (auto [b, _] : graph[a]) if (a < b) appearance[pair(a, b)] = 0; REP(i, q) { auto [type, a, B, c] = queries[i]; if (type == 1) { int b = int(B); if (a > b) swap(a, b); appearance[pair(a, b)] = i + 1; } else if (type == 2) { int b = int(B); if (a > b) swap(a, b); auto it = appearance.find(pair(a, b)); const int start = it->second; const int end = i + 1; appearance.erase(it); update(1, 0, sz - 1, start, end, a, b); } } for (auto [p, start] : appearance) { auto [a, b] = p; update(1, 0, sz - 1, start, q + 1, a, b); } queries.insert(queries.begin(), tuple<int, int, LL, int>{}); queries.resize(sz); function<void(int, int, int)> rec = [&](int w, int l, int r) { for (auto [a, b, time] : updates[w]) { fu.join(a, b, time); } if (w >= sz) { const int time = w - sz; auto [type, a, b, c] = queries[time]; if (type == 3) { fu.change_color(a, {time, c}); } else if (type == 4) { const auto [_, ans] = fu.get_color(a); cout << ans << '\n'; } } else { const int mid = (l + r) / 2; rec(w * 2, l, mid); rec(w * 2 + 1, mid + 1, r); } REP(i, ssize(updates[w])) fu.rollback(); }; rec(1, 0, sz - 1); } } // FU int main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> m >> q; graph.resize(n); REP(i, m) { int a, b, d; cin >> a >> b >> d; --a, --b; graph[a].emplace(b, d); graph[b].emplace(a, d); edge_cost[pair(a, b)] = d; edge_cost[pair(b, a)] = d; } debug(n, m, q, graph, edge_cost); queries.resize(q); for (auto& [type, a, b, c] : queries) { cin >> type; if (type == 1) { cin >> a >> b >> c; --a, --b; } else if (type == 2) { cin >> a >> b; --a, --b; } else if (type == 3) { cin >> a >> b >> c; --a; } else if (type == 4) { cin >> a; --a; } else { assert(false); } } debug(queries); [&] { if (m != n - 1) return; for (auto [type, a, b, c] : queries) if (type == 1 or type == 2) return; centro::solve(); exit(0); }(); [&] { const LL INF = 1e15; for (auto [type, a, b, c] : queries) if (type == 3 and b != INF) return; FU::solve(); exit(0); }(); brute::solve(); }
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto operator<<(auto&o,auto x)->decltype(x.end(),o); auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<", "<<get<3>(t)<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif int n, m, q; vector<set<pair<int, int>>> graph; map<pair<int, int>, int> edge_cost; vector<tuple<int, int, LL, int>> queries; namespace brute { int time = 0; int k; vector<int> color; vector<int> visited; void dfs(int v, LL z) { visited[v] = time; color[v] = k; for (auto [b, d] : graph[v]) if (visited[b] != time and d <= z) dfs(b, z - d); } void solve() { color.resize(n); visited.resize(n); for (auto [type, a, b, c] : queries) { if (type == 1) { graph[a].emplace(b, c); graph[b].emplace(a, c); edge_cost[pair(a, b)] = c; edge_cost[pair(b, a)] = c; } else if (type == 2) { const int d = edge_cost[pair(a, b)]; graph[a].erase(pair(b, d)); graph[b].erase(pair(a, d)); } else if (type == 3) { ++time; k = c; dfs(a, b); } else { assert(type == 4); cout << color[a] << '\n'; } } } } // brute using Info = pair<int, int>; // {time, color} namespace centro { struct Tree { int sz = 1; vector<Info> tree; Tree() {} Tree(int N) { while (sz < N) sz *= 2; tree.resize(2 * sz); } int l, r; Info val; void upd(int w, int a, int b) { if (a > r or b < l) return; if (a >= l and b <= r) { tree[w] = max(tree[w], val); return; } const int mid = (a + b) / 2; upd(w * 2, a, mid); upd(w * 2 + 1, mid + 1, b); } void update(int _l, int _r, Info _val) { l = _l; r = _r; val = _val; upd(1, 0, sz - 1); } Info query(int x) { Info ret = {0, 0}; x += sz; while (x) { ret = max(ret, tree[x]); x /= 2; } return ret; } }; vector<Tree> tree; vector<vector<LL>> dists; vector<vector<tuple<LL, int, int>>> centros; // {dist, centro, id_in_cetro} struct CentroDecomp { const vector<set<pair<int, int>>> &graph; // tu vector<int> par, podsz, odwi; int odwi_cnt = 1; const int INF = int(1e9); int root; void refresh() { ++odwi_cnt; } void visit(int v) { odwi[v] = max(odwi[v], odwi_cnt); } bool is_vis(int v) { return odwi[v] >= odwi_cnt; } void dfs_podsz(int v) { visit(v); podsz[v] = 1; for (auto [u, _] : graph[v]) // tu if (!is_vis(u)) { dfs_podsz(u); podsz[v] += podsz[u]; } } int centro(int v) { refresh(); dfs_podsz(v); int sz = podsz[v] / 2; refresh(); while (true) { visit(v); for (auto [u, _] : graph[v]) // tu if (!is_vis(u) && podsz[u] > sz) { v = u; break; } if (is_vis(v)) return v; } } void decomp(int v) { refresh(); // Tu kod. Centroid to v, ktory jest juz dozywotnie odwiedzony. vector<pair<LL, int>> children; function<void(int, LL)> dfs = [&](int a, LL len) { children.emplace_back(len, a); visit(a); for (auto [b, c] : graph[a]) { if (not is_vis(b)) { dfs(b, len + c); } } }; dfs(v, 0); sort(children.begin(), children.end()); const int s = ssize(children); tree[v] = Tree(s); REP(i, s) { auto [len, a] = children[i]; centros[a].emplace_back(len, v, i); dists[v].emplace_back(len); } // Koniec kodu. refresh(); for(auto [u, _] : graph[v]) // tu if (!is_vis(u)) { u = centro(u); par[u] = v; odwi[u] = INF; // Opcjonalnie tutaj przekazujemy info synowi w drzewie CD. decomp(u); } } CentroDecomp(int N, vector<set<pair<int, int>>> &grph) // tu : graph(grph), par(N, -1), podsz(N), odwi(N) { root = centro(0); odwi[root] = INF; decomp(root); } }; void solve() { tree.resize(n); dists.resize(n); centros.resize(n); CentroDecomp cd(n, graph); for (auto& v : centros) sort(v.begin(), v.end()); int time = 0; for (auto [type, a, b, c] : queries) { ++time; if (type == 3) { for (auto [dist, centro, _] : centros[a]) { if (dist > b) break; auto it = upper_bound(dists[centro].begin(), dists[centro].end(), b - dist); tree[centro].update(0, int(it - dists[centro].begin()) - 1, {time, c}); } } else { assert(type == 4); Info ans = {0, 0}; for (auto [_, centro, id_in_cetro] : centros[a]) { ans = max(ans, tree[centro].query(id_in_cetro)); } cout << ans.second << '\n'; } } } } // centro namespace FU { struct FindUnion { vector<Info> color; vector<int> size, par, par_time; vector<pair<int, int>> history; // {a, b} - a connected to b FindUnion(int N) : color(N, Info{-1, 0}), size(N, 1), par(N, -1), par_time(N, -1) {} int find(int a) { return par[a] == -1 ? a : find(par[a]); } void join(int a, int b, int time) { a = find(a); b = find(b); if (size[a] > size[b]) swap(a, b); par[a] = b; par_time[a] = time; size[b] += size[a]; history.emplace_back(a, b); } void rollback() { assert(not history.empty()); auto [a, b] = history.back(); history.pop_back(); par[a] = -1; size[b] -= size[a]; if (color[b].first >= par_time[a]) color[a] = max(color[a], color[b]); } void change_color(int a, Info val) { a = find(a); color[a] = max(color[a], val); } Info get_color(int a) { auto ret = color[a]; int time = -1; while (par[a] != -1) { time = max(time, par_time[a]); a = par[a]; if (color[a].first >= time) ret = max(ret, color[a]); } return ret; } }; int sz = 1; vector<vector<tuple<int, int, int>>> updates; // {a, b, time} void update(int w, int wa, int wb, int l, int r, int a, int b) { if (wa > r or wb < l) return; if (wa >= l and wb <= r) { updates[w].emplace_back(a, b, wa); return; } const int mid = (wa + wb) / 2; update(w * 2, wa, mid, l, r, a, b); update(w * 2 + 1, mid + 1, wb, l, r, a, b); } void solve() { FindUnion fu(n); while (sz < q + 2) sz *= 2; updates.resize(2 * sz); map<pair<int, int>, int> appearance; REP(a, n) for (auto [b, _] : graph[a]) if (a < b) appearance[pair(a, b)] = 0; REP(i, q) { auto [type, a, B, c] = queries[i]; if (type == 1) { int b = int(B); if (a > b) swap(a, b); appearance[pair(a, b)] = i + 1; } else if (type == 2) { int b = int(B); if (a > b) swap(a, b); auto it = appearance.find(pair(a, b)); const int start = it->second; const int end = i + 1; appearance.erase(it); update(1, 0, sz - 1, start, end, a, b); } } for (auto [p, start] : appearance) { auto [a, b] = p; update(1, 0, sz - 1, start, q + 1, a, b); } queries.insert(queries.begin(), tuple<int, int, LL, int>{}); queries.resize(sz); function<void(int, int, int)> rec = [&](int w, int l, int r) { for (auto [a, b, time] : updates[w]) { fu.join(a, b, time); } if (w >= sz) { const int time = w - sz; auto [type, a, b, c] = queries[time]; if (type == 3) { fu.change_color(a, {time, c}); } else if (type == 4) { const auto [_, ans] = fu.get_color(a); cout << ans << '\n'; } } else { const int mid = (l + r) / 2; rec(w * 2, l, mid); rec(w * 2 + 1, mid + 1, r); } REP(i, ssize(updates[w])) fu.rollback(); }; rec(1, 0, sz - 1); } } // FU int main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> m >> q; graph.resize(n); REP(i, m) { int a, b, d; cin >> a >> b >> d; --a, --b; graph[a].emplace(b, d); graph[b].emplace(a, d); edge_cost[pair(a, b)] = d; edge_cost[pair(b, a)] = d; } debug(n, m, q, graph, edge_cost); queries.resize(q); for (auto& [type, a, b, c] : queries) { cin >> type; if (type == 1) { cin >> a >> b >> c; --a, --b; } else if (type == 2) { cin >> a >> b; --a, --b; } else if (type == 3) { cin >> a >> b >> c; --a; } else if (type == 4) { cin >> a; --a; } else { assert(false); } } debug(queries); [&] { if (m != n - 1) return; for (auto [type, a, b, c] : queries) if (type == 1 or type == 2) return; centro::solve(); exit(0); }(); [&] { const LL INF = 1e15; for (auto [type, a, b, c] : queries) if (type == 3 and b != INF) return; FU::solve(); exit(0); }(); brute::solve(); } |