Submission #1278864

#TimeUsernameProblemLanguageResultExecution timeMemory
1278864janson0109Bridges (APIO19_bridges)C++20
73 / 100
3093 ms10408 KiB
#include <bits/stdc++.h> #define F first #define S second #define lol ios::sync_with_stdio(false);cin.tie(NULL); typedef int ll; typedef long double ld; using namespace std; const ll M = 998244353; #define p3 tuple<ll,ll,ll> #define p4 tuple<ll,ll,ll,ll> ll find(ll x, vector<ll> &p, vector<pair<ll,ll>> &ph, bool his) { if(p[x] == x) return x; //if(his) ph.push_back({x, p[x]}); return find(p[x], p, ph, his); //return p[x]; } bool unite(ll x, ll y, vector<ll> &p, vector<ll> &s, vector<pair<ll,ll>> &ph, vector<pair<ll,ll>> &sh, bool his) { ll xr = find(x, p, ph, his); ll yr = find(y, p, ph, his); if(xr == yr) return false; if(s[xr] < s[yr]) swap(xr,yr); if(his) { sh.push_back({xr, s[xr]}); ph.push_back({yr, p[yr]}); } s[xr]+=s[yr]; p[yr]=xr; return true; } void rollback(vector<ll> &p, vector<ll> &s, vector<pair<ll,ll>> &ph, vector<pair<ll,ll>> &sh) { while(!ph.empty()) { pair<ll,ll> pa = ph.back(); p[pa.F] = pa.S; ph.pop_back(); } while(!sh.empty()) { pair<ll,ll> pa = sh.back(); s[pa.F] = pa.S; sh.pop_back(); } } int main() { lol ll n, m; cin >> n >> m; vector<p3> e; set<p4, greater<p4>> es; for(ll i=0; i<m; i++) { ll u, v, d; cin >> u >> v >> d; u--;v--; e.push_back({d, u, v}); es.insert({d, u, v, i}); } ll q; cin >> q; vector<p3> qu(q); for(ll i=0; i<q; i++) cin >> get<0>(qu[i]) >> get<1>(qu[i]) >> get<2>(qu[i]); ll b = sqrtl(q); for(ll t=0; t < q; t+=b) { ll c = min(b, q-t); vector<bool> ch(m,0); for(ll i=0; i<c; i++) if(get<0>(qu[t+i]) == 1) ch[get<1>(qu[t+i]) - 1] = 1; vector<ll> mod; set<p4> mods; for(ll i=0; i<m; i++) if(ch[i]) {mod.push_back(i); mods.insert({get<0>(e[i]), get<1>(e[i]), get<2>(e[i]), i});} ll k = mod.size(); vector<ll> modm(m, -1); vector<ll> nd(k); for(ll i=0; i<k; i++) { nd[i] = get<0>(e[mod[i]]); modm[mod[i]] = i; } vector<vector<ll>> ndm(c); for(ll i=0; i<c; i++) { if(get<0>(qu[t+i]) == 1) nd[modm[get<1>(qu[t+i]) - 1]] = get<2>(qu[t+i]); else ndm[i] = nd; } vector<p3> qus; for(ll i=0; i<c; i++) if(get<0>(qu[t+i]) == 2) qus.push_back({get<2>(qu[t+i]), get<1>(qu[t+i])-1, i}); sort(qus.begin(), qus.end(), greater<p3>()); vector<ll> p(n), s(n,1); iota(p.begin(), p.end(), 0); vector<ll> ans(c, -1); vector<pair<ll,ll>> ph, sh; auto it = es.begin(); for(auto & quer : qus) { while(it != es.end()) { if(mods.find(*it) != mods.end()) it++; else if(get<0>(quer) <= get<0>(*it)) {unite(get<1>(*it), get<2>(*it), p, s, ph, sh, 0); it++;} else break; } for(ll j=0; j<k; j++) if(get<0>(quer) <= ndm[get<2>(quer)][j]) unite(get<1>(e[mod[j]]), get<2>(e[mod[j]]), p, s, ph, sh, 1); ans[get<2>(quer)] = s[find(get<1>(quer), p, ph, 1)]; rollback(p, s, ph, sh); } for(ll i=0; i<c; i++) if(get<0>(qu[t+i]) == 1) { ll j = get<1>(qu[t+i]) - 1; es.erase({get<0>(e[j]), get<1>(e[j]), get<2>(e[j]), j}); get<0>(e[j]) = get<2>(qu[t+i]); es.insert({get<0>(e[j]), get<1>(e[j]), get<2>(e[j]), j}); } for(ll i=0; i<c; i++) if(ans[i] != -1) cout << ans[i] << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...