結果

問題 No.3200 Sinking Islands
ユーザー Today03
提出日時 2025-07-12 20:55:29
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 2,817 bytes
コンパイル時間 3,355 ms
コンパイル使用メモリ 289,900 KB
実行使用メモリ 11,056 KB
最終ジャッジ日時 2025-07-12 20:55:39
合計ジャッジ時間 7,253 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(),(x).end()
#define REP(i, n) for(ll i=0; i<(ll)(n); i++)
#define PER(i, n) for(ll i=(ll)(n)-1; i>=0; i--)

template<typename T> int LB(const vector<T>& v, T x) { return lower_bound(ALL(v),x)-v.begin(); }
template<typename T> int UQ(T& v) { sort(ALL(v)); v.erase(unique(ALL(v)),v.end()); return v.size(); }
template<typename T> bool chmax(T &a, T b) { return a<b ? a=b, true : false; }
template<typename T> bool chmin(T &a, T b) { return a>b ? a=b, true : false; }
template<typename T> using rpriority_queue = priority_queue<T,vector<T>,greater<T>>;
using ll=long long; const int INF=1e9+10; const ll INFL=4e18;
using ld=long double; using lll=__int128_t; using ull=unsigned long long;
using VI=vector<int>; using VVI=vector<VI>; using VL=vector<ll>; using VVL=vector<VL>;
using PL=pair<ll,ll>; using VP=vector<PL>; using WG=vector<vector<pair<int,ll>>>;


#ifdef LOCAL
#include "./debug.hpp"
#else
#define debug(...)
#define print_line
#endif



/// @brief Disjoint Set Union
struct DSU {
    DSU()=default;

    DSU(int n) {
        par.resize(n); iota(par.begin(),par.end(),0);
        sz=vector<int>(n,1);
        forest_count=n;
    }

    int find(int x) {
        if(par[x]==x) return x;
        return par[x]=find(par[x]);
    }

    bool merge(int x, int y) {
        x=find(x); y=find(y);
        if(x==y) return false;
        if(sz[x]<sz[y]) swap(x,y);
        par[y]=x; sz[x]+=sz[y];
        forest_count--;
        return true;
    }

    int size(int x) { return sz[find(x)]; }

    bool same(int x, int y) { return find(x)==find(y); }

    int count() { return forest_count; }

    vector<vector<int>> groups() {
        int n=par.size();
        vector<vector<int>> ret(n);
        for(int i=0; i<n; i++) ret[find(i)].push_back(i);
        ret.erase(remove_if(ret.begin(),ret.end(),[&](const vector<int>& v) { return v.empty(); }),ret.end());
        return ret;
    }

private:
    vector<int> par,sz;
    int forest_count;
};

//----------------------------------------------------------

void solve() {
    ll N,M; cin>>N>>M;
    VP E(M); for(auto& [a,b]: E) cin>>a>>b, a--, b--;

    VI aru(M,1);
    int Q; cin>>Q;
    VI qry(Q);
    REP(i,Q) cin>>qry[i], qry[i]--, aru[qry[i]]=0;

    DSU dsu(N);
    ll tmp=N*(N-1)/2;
    REP(i,M) if(aru[i]) {
        auto [u,v]=E[i];
        if(!dsu.same(u,v)) tmp-=(ll)dsu.size(u)*dsu.size(v);
        dsu.merge(u,v);
    }

    VL ans(Q);
    PER(i,Q) {
        ans[i]=tmp;
        auto [u,v]=E[qry[i]];
        if(!dsu.same(u,v)) tmp-=(ll)dsu.size(u)*dsu.size(v);
        dsu.merge(u,v);
    }

    REP(i,Q) cout<<ans[i]<<'\n';
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    //cout<<fixed<<setprecision(15);
    int T=1;
    //cin>>T;
    while(T--) solve();
}
0