/* * Opis: Główny nagłówek */ #include<bits/stdc++.h> using namespace std; using LL=long long; #define st first #define nd second #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,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} 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 struct Edge { int to; LL len; int num; }; struct Graph { int n, m; vector<vector<Edge> > kraw; vector<bool> active; vector<int> col; vector<int> vis; int iter = 0; int cnt = 0; int max_val = 0; Graph(int _n, int _m, vector<vector<Edge> > &_kraw) { n = _n; m = _m; swap(kraw, _kraw); active.resize(m); col.resize(n); vis.resize(n); } void activate(int x) { active[x] = true; } void deactivate(int x) { active[x] = false; } void update(int x, LL dist, int c) { // zwieksz iter // debug(x, dist, c); vis[x] = iter; col[x] = c; for (Edge e : kraw[x]) { // debug(x, dist, e.to, e.len, e.num, iter); if (active[e.num] && vis[e.to] < iter && e.len <= dist) { update(e.to, dist - e.len, c); } } } int get(int x) { return col[x]; } pair<int, LL> get_node(int x, vector<vector<Edge> > &new_edges, vector<bool> &nodes, vector<int> &reserved, vector<int> &active_list) { vis[x] = iter; vector<pair<int, LL> > sons; for (auto e : kraw[x]) { if (active[e.to] && vis[e.to] < iter) { auto p = get_node(e.to, new_edges, nodes, reserved, active_list); if (p.st != -1) { p.nd += e.len; sons.push_back(p); } } } if (ssize(sons) >= 2) { nodes[x] = true; } if (nodes[x]) { for (auto p : sons) { while (!reserved.empty() && reserved.back() == cnt) { cnt++; reserved.pop_back(); } max_val = max(max_val, cnt); Edge e1{p.st, p.nd, cnt}; Edge e2{x, p.nd, cnt}; active_list.push_back(cnt); new_edges[x].push_back(e1); new_edges[p.st].push_back(e2); cnt++; } return {x, 0}; } else if (ssize(sons) == 1) { return sons.front(); } else { return {-1, -1}; } } }; struct EdgeFull { int from; int to; LL len; int num; }; bool operator<(const EdgeFull e1, const EdgeFull e2) { return tie(e1.from, e1.to, e1.len, e1.num) < tie(e2.from, e2.to, e2.len, e2.num); } struct Paint { int node; LL len; int col; }; // union EventData { // EdgeFull edge; // Paint paint; // int query; // }; struct Event { int event_type; EdgeFull edge; Paint paint; int query; }; Graph compress(Graph &g, vector<bool> &nodes, vector<int> &rmv, vector<EdgeFull> &rest) { vector<int> reserved; for (auto e : rest) { reserved.push_back(e.num); } sort(reserved.begin(), reserved.end()); reserved.erase(unique(reserved.begin(),reserved.end()), reserved.end()); sort(reserved.rbegin(), reserved.rend()); for (auto v : rmv) { g.deactivate(v); } vector<vector<Edge> > new_edges(g.n); g.iter++; g.cnt = 0; g.max_val = 0; if (!reserved.empty()) { g.max_val = reserved.front(); } vector<int> active_list; REP(i, g.n) { if (g.vis[i] < g.iter) { g.get_node(i, new_edges, nodes, reserved, active_list); } } for (auto e : rest) { int u = e.from; int v = e.to; LL len = e.len; int num = e.num; Edge e1{u, len, num}, e2{v, len, num}; new_edges[v].push_back(e1); new_edges[u].push_back(e2); } Graph comp(g.n, g.max_val+1, new_edges); comp.col = g.col; for (int v : active_list) { comp.activate(v); } for (auto v : rmv) { g.activate(v); } return comp; } const int W = 2; int main() { cin.tie(0)->sync_with_stdio(0); int n, m, q; cin >> n >> m >> q; vector<Event> events(m + q); map<pair<int, int>, int> edge_num; int curr_edge = 0; REP(i, m) { cin >> events[i].edge.from >> events[i].edge.to >> events[i].edge.len; events[i].edge.from--; events[i].edge.to--; events[i].edge.num = curr_edge; edge_num[{events[i].edge.from, events[i].edge.to}] = curr_edge; edge_num[{events[i].edge.to, events[i].edge.from}] = curr_edge; curr_edge++; } REP(i, q) { cin >> events[m + i].event_type; events[m + i].event_type--; int type = events[m + i].event_type; if (type == 0) { cin >> events[m + i].edge.from >> events[m + i].edge.to >> events[m + i].edge.len; events[m + i].edge.from--; events[m + i].edge.to--; events[m + i].edge.num = curr_edge; edge_num[{events[m + i].edge.from, events[m + i].edge.to}] = curr_edge; edge_num[{events[m + i].edge.to, events[m + i].edge.from}] = curr_edge; curr_edge++; } else if (type == 1) { cin >> events[m + i].edge.from >> events[m + i].edge.to; events[m + i].edge.from--; events[m + i].edge.to--; events[m + i].edge.num = edge_num[{events[m + i].edge.from, events[m + i].edge.to}]; events[m + i].edge.len = 0; } else if (type == 2) { cin >> events[m + i].paint.node >> events[m + i].paint.len >> events[m + i].paint.col; events[m + i].paint.node--; } else if (type == 3) { cin >> events[m + i].query; events[m + i].query--; } else { assert(false); } } vector<vector<Edge> > kraw(n); for (Event &ev : events) { if (ev.event_type == 0) { int u = ev.edge.from; int v = ev.edge.to; LL d = ev.edge.len; int num = ev.edge.num; kraw[u].push_back({v, d, num}); kraw[v].push_back({u, d, num}); } } Graph g(n, n+q, kraw); for (Event &ev : events) { // brute if (ev.event_type == 0) { g.activate(ev.edge.num); } else if (ev.event_type == 1) { g.deactivate(ev.edge.num); } else if (ev.event_type == 2) { g.iter++; g.update(ev.paint.node, ev.paint.len, ev.paint.col); } else if (ev.event_type == 3) { cout << g.get(ev.query) << "\n"; } } // edge_num.clear(); // vector<bool> nodes(n); // for (int i = 0; i < ssize(events); i += W) { // int iter = min(W, ssize(events) - i); // vector<int> rmv; // vector<EdgeFull> new_edges; // REP(j, iter) { // Event ev = events[i + j]; // if (ev.event_type == 0) { // nodes[ev.edge.from] = true; // nodes[ev.edge.to] = true; // } // else if (ev.event_type == 1) { // nodes[ev.edge.from] = true; // nodes[ev.edge.to] = true; // rmv.push_back(ev.edge.num); // if (edge_num.find({ev.edge.from, ev.edge.to}) != edge_num.end()) { // new_edges.push_back(ev.edge); // } // } // pamietac o usuwaniu z mapy // } // REP(j, iter) { // Event ev = events[i + j]; // if (ev.event_type == 0) { // new_edges.push_back(ev.edge); // } // } // Graph comp = compress(g, nodes, rmv, new_edges); // REP(j, iter) { // Event ev = events[i + j]; // if (ev.event_type == 0) { // comp.activate(ev.edge.num); // } // else if (ev.event_type == 1) { // comp.deactivate(ev.edge.num); // } // else if (ev.event_type == 2) { // comp.iter++; // comp.update(ev.paint.node, ev.paint.len, ev.paint.col); // } // else if (ev.event_type == 3) { // cout << comp.get(ev.query) << "\n"; // } // } // // debug("READY"); // REP(j, iter) { // Event ev = events[i + j]; // if (ev.event_type == 0) { // g.activate(ev.edge.num); // } // else if (ev.event_type == 1) { // g.deactivate(ev.edge.num); // } // } // g.iter++; // REP(j, iter) { // Event ev = events[i+iter-j-1]; // if (ev.event_type == 1) { // g.activate(ev.edge.num); // } // else if (ev.event_type == 0) { // g.deactivate(ev.edge.num); // } // else if (ev.event_type == 2 && g.vis[ev.paint.node] < g.iter) { // // debug(ev.paint.node, ev.paint.len, ev.paint.col); // // debug(g.active); // g.update(ev.paint.node, ev.paint.len, ev.paint.col); // } // } // REP(j, iter) { // Event ev = events[i + j]; // if (ev.event_type == 0) { // g.activate(ev.edge.num); // edge_num[{ev.edge.from, ev.edge.to}] = ev.edge.num; // edge_num[{ev.edge.to, ev.edge.from}] = ev.edge.num; // } // else if (ev.event_type == 1) { // g.deactivate(ev.edge.num); // edge_num.erase(edge_num.find({ev.edge.from, ev.edge.to})); // edge_num.erase(edge_num.find({ev.edge.to, ev.edge.from})); // } // } // debug(g.col); // } }
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 | /* * Opis: Główny nagłówek */ #include<bits/stdc++.h> using namespace std; using LL=long long; #define st first #define nd second #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,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} 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 struct Edge { int to; LL len; int num; }; struct Graph { int n, m; vector<vector<Edge> > kraw; vector<bool> active; vector<int> col; vector<int> vis; int iter = 0; int cnt = 0; int max_val = 0; Graph(int _n, int _m, vector<vector<Edge> > &_kraw) { n = _n; m = _m; swap(kraw, _kraw); active.resize(m); col.resize(n); vis.resize(n); } void activate(int x) { active[x] = true; } void deactivate(int x) { active[x] = false; } void update(int x, LL dist, int c) { // zwieksz iter // debug(x, dist, c); vis[x] = iter; col[x] = c; for (Edge e : kraw[x]) { // debug(x, dist, e.to, e.len, e.num, iter); if (active[e.num] && vis[e.to] < iter && e.len <= dist) { update(e.to, dist - e.len, c); } } } int get(int x) { return col[x]; } pair<int, LL> get_node(int x, vector<vector<Edge> > &new_edges, vector<bool> &nodes, vector<int> &reserved, vector<int> &active_list) { vis[x] = iter; vector<pair<int, LL> > sons; for (auto e : kraw[x]) { if (active[e.to] && vis[e.to] < iter) { auto p = get_node(e.to, new_edges, nodes, reserved, active_list); if (p.st != -1) { p.nd += e.len; sons.push_back(p); } } } if (ssize(sons) >= 2) { nodes[x] = true; } if (nodes[x]) { for (auto p : sons) { while (!reserved.empty() && reserved.back() == cnt) { cnt++; reserved.pop_back(); } max_val = max(max_val, cnt); Edge e1{p.st, p.nd, cnt}; Edge e2{x, p.nd, cnt}; active_list.push_back(cnt); new_edges[x].push_back(e1); new_edges[p.st].push_back(e2); cnt++; } return {x, 0}; } else if (ssize(sons) == 1) { return sons.front(); } else { return {-1, -1}; } } }; struct EdgeFull { int from; int to; LL len; int num; }; bool operator<(const EdgeFull e1, const EdgeFull e2) { return tie(e1.from, e1.to, e1.len, e1.num) < tie(e2.from, e2.to, e2.len, e2.num); } struct Paint { int node; LL len; int col; }; // union EventData { // EdgeFull edge; // Paint paint; // int query; // }; struct Event { int event_type; EdgeFull edge; Paint paint; int query; }; Graph compress(Graph &g, vector<bool> &nodes, vector<int> &rmv, vector<EdgeFull> &rest) { vector<int> reserved; for (auto e : rest) { reserved.push_back(e.num); } sort(reserved.begin(), reserved.end()); reserved.erase(unique(reserved.begin(),reserved.end()), reserved.end()); sort(reserved.rbegin(), reserved.rend()); for (auto v : rmv) { g.deactivate(v); } vector<vector<Edge> > new_edges(g.n); g.iter++; g.cnt = 0; g.max_val = 0; if (!reserved.empty()) { g.max_val = reserved.front(); } vector<int> active_list; REP(i, g.n) { if (g.vis[i] < g.iter) { g.get_node(i, new_edges, nodes, reserved, active_list); } } for (auto e : rest) { int u = e.from; int v = e.to; LL len = e.len; int num = e.num; Edge e1{u, len, num}, e2{v, len, num}; new_edges[v].push_back(e1); new_edges[u].push_back(e2); } Graph comp(g.n, g.max_val+1, new_edges); comp.col = g.col; for (int v : active_list) { comp.activate(v); } for (auto v : rmv) { g.activate(v); } return comp; } const int W = 2; int main() { cin.tie(0)->sync_with_stdio(0); int n, m, q; cin >> n >> m >> q; vector<Event> events(m + q); map<pair<int, int>, int> edge_num; int curr_edge = 0; REP(i, m) { cin >> events[i].edge.from >> events[i].edge.to >> events[i].edge.len; events[i].edge.from--; events[i].edge.to--; events[i].edge.num = curr_edge; edge_num[{events[i].edge.from, events[i].edge.to}] = curr_edge; edge_num[{events[i].edge.to, events[i].edge.from}] = curr_edge; curr_edge++; } REP(i, q) { cin >> events[m + i].event_type; events[m + i].event_type--; int type = events[m + i].event_type; if (type == 0) { cin >> events[m + i].edge.from >> events[m + i].edge.to >> events[m + i].edge.len; events[m + i].edge.from--; events[m + i].edge.to--; events[m + i].edge.num = curr_edge; edge_num[{events[m + i].edge.from, events[m + i].edge.to}] = curr_edge; edge_num[{events[m + i].edge.to, events[m + i].edge.from}] = curr_edge; curr_edge++; } else if (type == 1) { cin >> events[m + i].edge.from >> events[m + i].edge.to; events[m + i].edge.from--; events[m + i].edge.to--; events[m + i].edge.num = edge_num[{events[m + i].edge.from, events[m + i].edge.to}]; events[m + i].edge.len = 0; } else if (type == 2) { cin >> events[m + i].paint.node >> events[m + i].paint.len >> events[m + i].paint.col; events[m + i].paint.node--; } else if (type == 3) { cin >> events[m + i].query; events[m + i].query--; } else { assert(false); } } vector<vector<Edge> > kraw(n); for (Event &ev : events) { if (ev.event_type == 0) { int u = ev.edge.from; int v = ev.edge.to; LL d = ev.edge.len; int num = ev.edge.num; kraw[u].push_back({v, d, num}); kraw[v].push_back({u, d, num}); } } Graph g(n, n+q, kraw); for (Event &ev : events) { // brute if (ev.event_type == 0) { g.activate(ev.edge.num); } else if (ev.event_type == 1) { g.deactivate(ev.edge.num); } else if (ev.event_type == 2) { g.iter++; g.update(ev.paint.node, ev.paint.len, ev.paint.col); } else if (ev.event_type == 3) { cout << g.get(ev.query) << "\n"; } } // edge_num.clear(); // vector<bool> nodes(n); // for (int i = 0; i < ssize(events); i += W) { // int iter = min(W, ssize(events) - i); // vector<int> rmv; // vector<EdgeFull> new_edges; // REP(j, iter) { // Event ev = events[i + j]; // if (ev.event_type == 0) { // nodes[ev.edge.from] = true; // nodes[ev.edge.to] = true; // } // else if (ev.event_type == 1) { // nodes[ev.edge.from] = true; // nodes[ev.edge.to] = true; // rmv.push_back(ev.edge.num); // if (edge_num.find({ev.edge.from, ev.edge.to}) != edge_num.end()) { // new_edges.push_back(ev.edge); // } // } // pamietac o usuwaniu z mapy // } // REP(j, iter) { // Event ev = events[i + j]; // if (ev.event_type == 0) { // new_edges.push_back(ev.edge); // } // } // Graph comp = compress(g, nodes, rmv, new_edges); // REP(j, iter) { // Event ev = events[i + j]; // if (ev.event_type == 0) { // comp.activate(ev.edge.num); // } // else if (ev.event_type == 1) { // comp.deactivate(ev.edge.num); // } // else if (ev.event_type == 2) { // comp.iter++; // comp.update(ev.paint.node, ev.paint.len, ev.paint.col); // } // else if (ev.event_type == 3) { // cout << comp.get(ev.query) << "\n"; // } // } // // debug("READY"); // REP(j, iter) { // Event ev = events[i + j]; // if (ev.event_type == 0) { // g.activate(ev.edge.num); // } // else if (ev.event_type == 1) { // g.deactivate(ev.edge.num); // } // } // g.iter++; // REP(j, iter) { // Event ev = events[i+iter-j-1]; // if (ev.event_type == 1) { // g.activate(ev.edge.num); // } // else if (ev.event_type == 0) { // g.deactivate(ev.edge.num); // } // else if (ev.event_type == 2 && g.vis[ev.paint.node] < g.iter) { // // debug(ev.paint.node, ev.paint.len, ev.paint.col); // // debug(g.active); // g.update(ev.paint.node, ev.paint.len, ev.paint.col); // } // } // REP(j, iter) { // Event ev = events[i + j]; // if (ev.event_type == 0) { // g.activate(ev.edge.num); // edge_num[{ev.edge.from, ev.edge.to}] = ev.edge.num; // edge_num[{ev.edge.to, ev.edge.from}] = ev.edge.num; // } // else if (ev.event_type == 1) { // g.deactivate(ev.edge.num); // edge_num.erase(edge_num.find({ev.edge.from, ev.edge.to})); // edge_num.erase(edge_num.find({ev.edge.to, ev.edge.from})); // } // } // debug(g.col); // } } |