#include <bits/stdc++.h> #define all(x) (x).begin(),(x).end() using namespace std; using ll = long long; using ld = long double; #define int ll #define sz(x) ((int)(x).size()) using pii = pair<int,int>; using tii = tuple<int,int,int>; // o sa spargem pe ala cu cea mai mica prioritate pt ca asta are sens lol const int qmax = 4e5 + 5; const int nmax = 2e5 + 5; namespace Substakul2 { namespace DSU { int dsu[nmax]; pii local[nmax]; // timpul + culoarea int link_req[nmax]; int timer = 0; vector<tii> st; void init(int n) { for(int i = 1; i <= n; i++) dsu[i] = -1; } int f(int x) { return dsu[x] < 0? x : f(dsu[x]); } void unite(int x, int y) { x = f(x); y = f(y); if(x == y) return; // ??? if(-dsu[x] < -dsu[y]) swap(x, y); st.emplace_back(x, y, dsu[y]); link_req[y] = ++timer; dsu[x] += dsu[y]; dsu[y] = x; return; } void color_comp(int x, int c) { x = f(x); local[x] = pii{++timer, c}; } void undo() { auto [x, y, prv] = st.back(); //#warning Exista probabil mai multe nuante decat vreau sa analizez aici. Poate fi sursa de buguri if(link_req[y] < local[x].first) local[y] = local[x]; dsu[y] = prv; dsu[x] -= prv; st.pop_back(); } int query(int x) { //cerr << x << ' '; int atr = 0; int limit = -1; while(x > 0) { //cerr << ": " << x << ' ' << local[x].second << '\t'; if(local[x].first >= limit) atr = max(atr, local[x].second); limit = max(limit, link_req[x]); x = dsu[x]; } //cerr << atr << '\n'; return atr; } } int actual_color[qmax]; namespace PQ_Undo { set<pii> bypri; vector<pair<pii, int>> st; vector<int> pos_inst, atrpri; vector<pii> atrupd; void push(pii upd, int pri) { //cerr << " + " << sz(pos_inst) << '\n'; atrpri.emplace_back(pri); atrupd.emplace_back(upd); DSU::unite(upd.first, upd.second); st.emplace_back(make_pair(upd, sz(pos_inst))); pos_inst.emplace_back(sz(st) - 1); bypri.emplace(make_pair(pri, sz(pos_inst) - 1)); } void pop() { int k = sz(st), last_priority; vector<pair<pii, int>> big, small; auto it = bypri.begin(); for(int qt = 1, lol = 0; !lol || qt < (sz(st) + 1) / 2; lol = 1, qt++) { int individ = it -> second; //cerr << "wow\t" << individ << ' ' << pos_inst[individ] << '\n'; k = min(pos_inst[individ], k); if(qt > 1) big.emplace_back(make_pair(atrupd[individ], individ)); last_priority = atrpri[individ]; if(qt * 2 > sz(st) - k) break; it++; } //cerr << atrupd[bypri.begin() -> second].first << ' ' << atrupd[bypri.begin() -> second].second << '\n'; //cerr << k << ' ' << sz(st) << '\n'; bypri.erase(bypri.begin()); while(sz(st) > k) { auto [upd, individ] = st.back(); st.pop_back(); DSU::undo(); if(atrpri[individ] > last_priority) small.emplace_back(make_pair(upd, individ)); } copy(rbegin(big), rend(big), back_inserter(small)); for(auto [upd, individ] : small) { DSU::unite(upd.first, upd.second); st.emplace_back(make_pair(upd, individ)); pos_inst[individ] = sz(st) - 1; } //cerr << "\n\n-----Raman:\n"; //for(auto [a, b] : st) cerr << b << ' '; //cerr << '\n'; //for(auto [a, b] : bypri) cerr << b << ' '; //cerr << '\n'; //cerr << "====\n"; } } } namespace Subtaskul1 { list<pii> g[nmax]; map<pii, pair<list<pii>::iterator, list<pii>::iterator>> atredg; int color[nmax]; void init(int node, int f, int ncolor, int remaining) { if(remaining < 0) return; color[node] = ncolor; for(auto [x, c] : g[node]) { if(x != f) init(x, node, ncolor, remaining - c); } return; } } struct Update { int type, x, y, C; int priority; }; signed main() { cin.tie(0) -> sync_with_stdio(0); int n, m, q; cin >> n >> m >> q; map<pii, int> edgptr; vector<Update> upds; for(int i = 0, x, y, d; i < m; i++) { cin >> x >> y >> d; if(x > y) swap(x, y); edgptr[pii{x, y}] = sz(upds); upds.emplace_back(1, x, y, d, -1); } for(int i = 0, t, x, y, d; i < q; i++) { cin >> t; if(t == 1) { cin >> x >> y >> d; if(x > y) swap(x, y); edgptr[pii{x, y}] = sz(upds); upds.emplace_back(1, x, y, d, -1); } else if(t == 2) { cin >> x >> y; if(x > y) swap(x, y); upds[edgptr[pii{x, y}]].priority = i; edgptr.erase(pii{x, y}); upds.emplace_back(2, x, y, -1, -1); } else if(t == 3) { cin >> x >> d >> y; upds.emplace_back(3, x, d, y, -1); } else { cin >> x; upds.emplace_back(4, x, -1, -1, -1); } } int cnt = q; for(auto [a, b] : edgptr) upds[b].priority = ++cnt; // am citit, acum sa bagam efectiv cod // ------------- if([&]() { for(auto &[type, x, y, C, pri] : upds) { if(type == 3) { if(y != (ll)(1e15)) return 0; } } return 1; }()) { using namespace Substakul2; int color = 0; DSU::init(n + 2); for(auto [type, x, y, C, pri] : upds) { switch(type) { case 1: { PQ_Undo::push(pii{x, y}, pri); break; } case 2: { PQ_Undo::pop(); break; } case 3: { ++color; DSU::color_comp(x, color); actual_color[color] = C; break; } case 4: { int u = DSU::query(x); cout << actual_color[u] << '\n'; break; } } } } else { using namespace Subtaskul1; for(auto [t, x, y, C, pri] : upds) { if(t == 1) { g[x].emplace_back(y, C); g[y].emplace_back(x, C); if(x > y) swap(x, y); atredg[pii{x, y}] = make_pair(prev(g[x].end()), prev(g[y].end())); } else if(t == 2) { if(x > y) swap(x, y); auto [it1, it2] = atredg[pii{x, y}]; g[x].erase(it1); g[y].erase(it2); } else if(t == 3) { int v = x, z = y, k = C; init(v, v, k, z); } else { int v = x; cout << color[v] << '\n'; } } } } /** Anul asta nu se da centroid -- Rugaciunile mele */
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 | #include <bits/stdc++.h> #define all(x) (x).begin(),(x).end() using namespace std; using ll = long long; using ld = long double; #define int ll #define sz(x) ((int)(x).size()) using pii = pair<int,int>; using tii = tuple<int,int,int>; // o sa spargem pe ala cu cea mai mica prioritate pt ca asta are sens lol const int qmax = 4e5 + 5; const int nmax = 2e5 + 5; namespace Substakul2 { namespace DSU { int dsu[nmax]; pii local[nmax]; // timpul + culoarea int link_req[nmax]; int timer = 0; vector<tii> st; void init(int n) { for(int i = 1; i <= n; i++) dsu[i] = -1; } int f(int x) { return dsu[x] < 0? x : f(dsu[x]); } void unite(int x, int y) { x = f(x); y = f(y); if(x == y) return; // ??? if(-dsu[x] < -dsu[y]) swap(x, y); st.emplace_back(x, y, dsu[y]); link_req[y] = ++timer; dsu[x] += dsu[y]; dsu[y] = x; return; } void color_comp(int x, int c) { x = f(x); local[x] = pii{++timer, c}; } void undo() { auto [x, y, prv] = st.back(); //#warning Exista probabil mai multe nuante decat vreau sa analizez aici. Poate fi sursa de buguri if(link_req[y] < local[x].first) local[y] = local[x]; dsu[y] = prv; dsu[x] -= prv; st.pop_back(); } int query(int x) { //cerr << x << ' '; int atr = 0; int limit = -1; while(x > 0) { //cerr << ": " << x << ' ' << local[x].second << '\t'; if(local[x].first >= limit) atr = max(atr, local[x].second); limit = max(limit, link_req[x]); x = dsu[x]; } //cerr << atr << '\n'; return atr; } } int actual_color[qmax]; namespace PQ_Undo { set<pii> bypri; vector<pair<pii, int>> st; vector<int> pos_inst, atrpri; vector<pii> atrupd; void push(pii upd, int pri) { //cerr << " + " << sz(pos_inst) << '\n'; atrpri.emplace_back(pri); atrupd.emplace_back(upd); DSU::unite(upd.first, upd.second); st.emplace_back(make_pair(upd, sz(pos_inst))); pos_inst.emplace_back(sz(st) - 1); bypri.emplace(make_pair(pri, sz(pos_inst) - 1)); } void pop() { int k = sz(st), last_priority; vector<pair<pii, int>> big, small; auto it = bypri.begin(); for(int qt = 1, lol = 0; !lol || qt < (sz(st) + 1) / 2; lol = 1, qt++) { int individ = it -> second; //cerr << "wow\t" << individ << ' ' << pos_inst[individ] << '\n'; k = min(pos_inst[individ], k); if(qt > 1) big.emplace_back(make_pair(atrupd[individ], individ)); last_priority = atrpri[individ]; if(qt * 2 > sz(st) - k) break; it++; } //cerr << atrupd[bypri.begin() -> second].first << ' ' << atrupd[bypri.begin() -> second].second << '\n'; //cerr << k << ' ' << sz(st) << '\n'; bypri.erase(bypri.begin()); while(sz(st) > k) { auto [upd, individ] = st.back(); st.pop_back(); DSU::undo(); if(atrpri[individ] > last_priority) small.emplace_back(make_pair(upd, individ)); } copy(rbegin(big), rend(big), back_inserter(small)); for(auto [upd, individ] : small) { DSU::unite(upd.first, upd.second); st.emplace_back(make_pair(upd, individ)); pos_inst[individ] = sz(st) - 1; } //cerr << "\n\n-----Raman:\n"; //for(auto [a, b] : st) cerr << b << ' '; //cerr << '\n'; //for(auto [a, b] : bypri) cerr << b << ' '; //cerr << '\n'; //cerr << "====\n"; } } } namespace Subtaskul1 { list<pii> g[nmax]; map<pii, pair<list<pii>::iterator, list<pii>::iterator>> atredg; int color[nmax]; void init(int node, int f, int ncolor, int remaining) { if(remaining < 0) return; color[node] = ncolor; for(auto [x, c] : g[node]) { if(x != f) init(x, node, ncolor, remaining - c); } return; } } struct Update { int type, x, y, C; int priority; }; signed main() { cin.tie(0) -> sync_with_stdio(0); int n, m, q; cin >> n >> m >> q; map<pii, int> edgptr; vector<Update> upds; for(int i = 0, x, y, d; i < m; i++) { cin >> x >> y >> d; if(x > y) swap(x, y); edgptr[pii{x, y}] = sz(upds); upds.emplace_back(1, x, y, d, -1); } for(int i = 0, t, x, y, d; i < q; i++) { cin >> t; if(t == 1) { cin >> x >> y >> d; if(x > y) swap(x, y); edgptr[pii{x, y}] = sz(upds); upds.emplace_back(1, x, y, d, -1); } else if(t == 2) { cin >> x >> y; if(x > y) swap(x, y); upds[edgptr[pii{x, y}]].priority = i; edgptr.erase(pii{x, y}); upds.emplace_back(2, x, y, -1, -1); } else if(t == 3) { cin >> x >> d >> y; upds.emplace_back(3, x, d, y, -1); } else { cin >> x; upds.emplace_back(4, x, -1, -1, -1); } } int cnt = q; for(auto [a, b] : edgptr) upds[b].priority = ++cnt; // am citit, acum sa bagam efectiv cod // ------------- if([&]() { for(auto &[type, x, y, C, pri] : upds) { if(type == 3) { if(y != (ll)(1e15)) return 0; } } return 1; }()) { using namespace Substakul2; int color = 0; DSU::init(n + 2); for(auto [type, x, y, C, pri] : upds) { switch(type) { case 1: { PQ_Undo::push(pii{x, y}, pri); break; } case 2: { PQ_Undo::pop(); break; } case 3: { ++color; DSU::color_comp(x, color); actual_color[color] = C; break; } case 4: { int u = DSU::query(x); cout << actual_color[u] << '\n'; break; } } } } else { using namespace Subtaskul1; for(auto [t, x, y, C, pri] : upds) { if(t == 1) { g[x].emplace_back(y, C); g[y].emplace_back(x, C); if(x > y) swap(x, y); atredg[pii{x, y}] = make_pair(prev(g[x].end()), prev(g[y].end())); } else if(t == 2) { if(x > y) swap(x, y); auto [it1, it2] = atredg[pii{x, y}]; g[x].erase(it1); g[y].erase(it2); } else if(t == 3) { int v = x, z = y, k = C; init(v, v, k, z); } else { int v = x; cout << color[v] << '\n'; } } } } /** Anul asta nu se da centroid -- Rugaciunile mele */ |