結果

問題 No.3200 Sinking Islands
ユーザー theory_and_me
提出日時 2025-07-12 03:55:52
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 98 ms / 2,000 ms
コード長 7,420 bytes
コンパイル時間 2,429 ms
コンパイル使用メモリ 203,880 KB
実行使用メモリ 9,400 KB
最終ジャッジ日時 2025-07-12 03:55:59
合計ジャッジ時間 6,865 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define rep_(i, a_, b_, a, b, ...) for (int i = (a), lim##i = (b); i < lim##i; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define drep_(i, a_, b_, a, b, ...) for (int i = (a) - 1, lim##i = (b); i >= lim##i; --i)
#define drep(i, ...) drep_(i, __VA_ARGS__, __VA_ARGS__, __VA_ARGS__, 0)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#ifdef LOCAL
void debug_out() {
    cerr << endl;
}
template <class Head, class... Tail> void debug_out(Head H, Tail... T) {
    cerr << ' ' << H;
    debug_out(T...);
}
#define debug(...) cerr << 'L' << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << 'L' << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using ll = long long;
using ld = long double;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
template <class T> vector<T> make_vec(size_t n, T a) {
    return vector<T>(n, a);
}
template <class... Ts> auto make_vec(size_t n, Ts... ts) {
    return vector<decltype(make_vec(ts...))>(n, make_vec(ts...));
}
template <class T> inline void fin(const T x) {
    cout << x << '\n';
    exit(0);
}
template <class T> inline void deduplicate(vector<T> &a) {
    sort(all(a));
    a.erase(unique(all(a)), a.end());
}
template <class T> inline bool chmin(T &a, const T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> inline bool chmax(T &a, const T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> inline int sz(const T &x) {
    return x.size();
}
template <class T> inline int count_between(const vector<T> &a, T l, T r) {
    return lower_bound(all(a), r) - lower_bound(all(a), l);
}
template <class T1, class T2> istream &operator>>(istream &is, pair<T1, T2> &p) {
    is >> p.first >> p.second;
    return is;
}
template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p) {
    os << '(' << p.first << ", " << p.second << ')';
    return os;
}
template <class T, size_t n> istream &operator>>(istream &is, array<T, n> &v) {
    for (auto &e : v) is >> e;
    return is;
}
template <class T, size_t n> ostream &operator<<(ostream &os, array<T, n> &v) {
    for (auto &e : v) os << e << ' ';
    return os;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
    for (auto &e : v) is >> e;
    return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
    for (auto &e : v) os << e << ' ';
    return os;
}
template <class T> istream &operator>>(istream &is, deque<T> &v) {
    for (auto &e : v) is >> e;
    return is;
}
template <class T> ostream &operator<<(ostream &os, deque<T> &v) {
    for (auto &e : v) os << e << ' ';
    return os;
}
inline ll floor_div(ll x, ll y) {
    if (y < 0) x = -x, y = -y;
    return x >= 0 ? x / y : (x - y + 1) / y;
}
inline ll ceil_div(ll x, ll y) {
    if (y < 0) x = -x, y = -y;
    return x >= 0 ? (x + y - 1) / y : x / y;
}
inline int floor_log2(const ll x) {
    assert(x > 0);
    return 63 - __builtin_clzll(x);
}
inline int ceil_log2(const ll x) {
    assert(x > 0);
    return (x == 1) ? 0 : 64 - __builtin_clzll(x - 1);
}
inline int popcount(const ll x) {
    return __builtin_popcountll(x);
}
struct fast_ios {
    fast_ios() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(20);
    };
} fast_ios;

// 時間計測
auto system_now = std::chrono::system_clock::now();
int check_time() {
    auto now = std::chrono::system_clock::now();
    return std::chrono::duration_cast<std::chrono::milliseconds>(now - system_now).count();
}

// 乱数
struct Xorshift {
    uint32_t x = 123456789, y = 362436069, z = 521288629, w = 88675123;

    uint32_t rand_int() {
        uint32_t t = x ^ (x << 11);
        x = y;
        y = z;
        z = w;
        return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    }

    // 0以上mod未満の整数を乱択
    uint32_t rand_int(uint32_t mod) {
        return rand_int() % mod;
    }

    // l以上r未満の整数を乱択
    uint32_t rand_int(uint32_t l, uint32_t r) {
        assert(l < r);
        return l + rand_int(r - l);
    }

    // 0以上1以下の実数を乱沢
    double rand_double() {
        return (double)rand_int() / UINT32_MAX;
    }
};
Xorshift xor_shift;

// constexpr int INF = numeric_limits<int>::max() >> 2;
// constexpr ll INFll = numeric_limits<ll>::max() >> 2;
// constexpr ld EPS = 1e-10;
// const ld PI = acos(-1.0);
// using mint = modint998244353;
// using mint = modint1000000007;
// using mint = modint;
// using Vm = V<mint>; using VVm = VV<mint>;

// par[i]  は,iがrootなら-(連結成分のサイズ)を,iがrootではないならばrootを返す
// root(x) は必ずroot を返す
struct UnionFindSize {
    vector<int> par;

    UnionFindSize(int n)
        : par(n, -1) {
    }
    void init(int n) {
        par.assign(n, -1);
    }

    int root(int x) {
        if (par[x] < 0)
            return x;
        else
            return par[x] = root(par[x]);
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    bool merge(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y);  // merge technique
        par[x] += par[y];
        par[y] = x;
        return true;
    }

    int size(int x) {
        return -par[root(x)];
    }

    void print() {
        cout << "uf: ";
        for (int i = 0; i < (int)par.size(); i++) cout << root(i) << " ";
        cout << endl;
    }
};

int main() {
    int N, M;
    cin >> N >> M;
    vector<int> U(M), V(M);
    rep(i, M) {
        cin >> U[i] >> V[i];
        --U[i], --V[i];  // 0-indexed
    }
    int Q;
    cin >> Q;
    vector<int> B(Q);
    rep(i, Q) {
        cin >> B[i];
        --B[i];  // 0-indexed
    }

    UnionFindSize uf(N);
    vector<int> edge_resid(M, 1);
    rep(i, Q) {
        edge_resid[B[i]] = 0;  // B[i]の辺を削除
    }
    rep(i, M) {
        if (edge_resid[i]) {  // 削除されていない辺
            uf.merge(U[i], V[i]);
        }
    }

    ll ans = (ll)N * (N - 1) / 2;
    rep(i, N) {
        if (uf.root(i) == i) {  // 連結成分の代表
            ll size = uf.size(i);
            ans -= size * (size - 1) / 2;  // この連結成分の頂点間の距離は0
        }
    }

    vector<ll> res;
    res.push_back(ans);

    rep(i, Q - 1) {
        int idx = B[Q - 1 - i];  // 削除された辺を復元;
        if (uf.issame(U[idx], V[idx])) {
            res.push_back(res.back());
        } else {
            // 新たに連結されるので、距離を更新
            ll sz_1 = uf.size(uf.root(U[idx]));
            ll sz_2 = uf.size(uf.root(V[idx]));
            ans += sz_1 * (sz_1 - 1) / 2;  // 連結成分の頂点間の距離は0
            ans += sz_2 * (sz_2 - 1) / 2;  // 連結成分の頂点間の距離は0
            uf.merge(U[idx], V[idx]);
            ll size = uf.size(uf.root(U[idx]));
            ans -= size * (size - 1) / 2;  // この連結成分の頂点間の距離は0
            res.push_back(ans);
        }
    }
    reverse(all(res));
    for (const auto &e : res) {
        cout << e << '\n';
    }

    return 0;
}
0