結果

問題 No.3200 Sinking Islands
ユーザー Hyoka7
提出日時 2025-07-11 22:19:31
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 691 ms / 2,000 ms
コード長 2,089 bytes
コンパイル時間 2,697 ms
コンパイル使用メモリ 226,156 KB
実行使用メモリ 21,248 KB
最終ジャッジ日時 2025-07-11 22:19:50
合計ジャッジ時間 15,664 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < (int)(n); i++)
using ll = long long;

template <typename T>
struct UnionFind
{
    vector<T> parent, rank, siz;
    UnionFind(T n) : parent(n, -1), rank(n, 0), siz(n, 1) {}
    T root(T x)
    {
        if (parent[x] == -1)
        {
            return x;
        }
        else
        {
            return parent[x] = root(parent[x]);
        }
    }
    bool same(T x, T y)
    {
        return root(x) == root(y);
    }
    bool merge(T x, T y)
    {
        T root_x = root(x), root_y = root(y);
        if (root_x == root_y)
        {
            return false;
        }
        if (rank[root_x] < rank[root_y])
        {
            swap(root_x, root_y);
        }
        parent[root_y] = root_x;
        if (rank[root_x] == rank[root_y])
        {
            rank[root_x]++;
        }
        siz[root_x] += siz[root_y];
        return true;
    }
    T size(int x)
    {
        return siz[root(x)];
    }
};

int main()
{
    ll n, m;
    cin >> n >> m;
    UnionFind uf(n);
    vector<int> u(m), v(m);
    rep(i, 0, m)
    {
        cin >> u[i] >> v[i];
        u[i]--;
        v[i]--;
    }
    set<int> destructed;
    int q;
    cin >> q;
    vector<int> jyunban(q);
    rep(i, 0, q)
    {
        cin >> jyunban[i];
        jyunban[i]--;
        destructed.insert(jyunban[i]);
    }
    ll ans = n * (n - 1) / 2;
    rep(i, 0, m)
    {
        if (!destructed.count(i))
        {
            ll usize = uf.size(u[i]), vsize = uf.size(v[i]);
            if (!uf.same(u[i], v[i]))
            {
                ans -= usize * vsize;
                uf.merge(u[i], v[i]);
            }
        }
    }
    vector<ll> answers(q);
    for (int i = q - 1; i >= 0; --i)
    {
        answers[i] = ans;
        int idx = jyunban[i];
        int a = u[idx], b = v[idx];
        if (!uf.same(a, b))
        {
            ans -= (ll)uf.size(a) * uf.size(b);
            uf.merge(a, b);
        }
    }
    rep(i, 0, q)
    {
        cout << answers[i] << endl;
    }
}
0