Submission #1278792

#TimeUsernameProblemLanguageResultExecution timeMemory
1278792janson0109Bridges (APIO19_bridges)C++20
30 / 100
3094 ms19564 KiB
#include <bits/stdc++.h>
#define F first
#define S second
#define lol ios::sync_with_stdio(false);cin.tie(NULL);
typedef long long ll;
typedef long double ld;
using namespace std;
const ll M = 998244353;

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]});
    p[x] = 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<vector<ll>> e;
    for(ll i=0; i<m; i++) {
        ll u, v, d;
        cin >> u >> v >> d;
        u--;v--;
        e.push_back({d, u, v});
    }
    ll q; cin >> q;
    vector<vector<ll>> qu(q, vector<ll>(3));
    for(ll i=0; i<q; i++) for(ll j=0; j<3; j++) cin >> qu[i][j];
    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(qu[t+i][0] == 1) ch[qu[t+i][1] - 1] = 1;
        vector<ll> mod;
        for(ll i=0; i<m; i++) if(ch[i]) mod.push_back(i);
        ll k = mod.size(); map<ll,ll> modm;
        vector<ll> nd(k);
        for(ll i=0; i<k; i++) {
            nd[i] = e[mod[i]][0];
            modm[mod[i]] = i;
        }
        vector<vector<ll>> ndm(c);
        for(ll i=0; i<c; i++) {
            if(qu[t+i][0] == 1) nd[modm[qu[t+i][1] - 1]] = qu[t+i][2];
            else ndm[i] = nd;
        }

        vector<vector<ll>> qus;
        for(ll i=0; i<c; i++) if(qu[t+i][0] == 2) qus.push_back({qu[t+i][2], qu[t+i][1]-1, i});
        sort(qus.begin(), qus.end(), greater<vector<ll>>());
        vector<vector<ll>> uc;
        for(ll i=0; i<m; i++) if(!ch[i]) uc.push_back(e[i]);
        sort(uc.begin(), uc.end());

        vector<ll> p(n), s(n,1);
        iota(p.begin(), p.end(), 0);
        vector<ll> ans(c, -1);
        auto it = uc.rbegin();
        vector<pair<ll,ll>> ph, sh;
        for(auto & quer : qus) {
            while(it != uc.rend() && quer[0] <= (*it)[0]) {
                unite((*it)[1], (*it)[2], p, s, ph, sh, 0);
                it++;
            }
            for(ll j=0; j<k; j++) if(quer[0] <= ndm[quer[2]][j]) unite(e[mod[j]][1], e[mod[j]][2], p, s, ph, sh, 1);
            ans[quer[2]] = s[find(quer[1], p, ph, 1)];
            rollback(p, s, ph, sh);
        }
        for(ll i=0; i<c; i++) if(qu[t+i][0] == 1) e[qu[t+i][1] - 1][0] = qu[t+i][2];
        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...