提出 #66951980


ソースコード 拡げる

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

struct DSU {
    vector<int> p, sz;
    DSU(int n): p(n+1), sz(n+1,1) { iota(p.begin(), p.end(), 0); }
    int find(int x) { return p[x]==x ? x : p[x] = find(p[x]); }
    int unite(int a, int b) {
        a = find(a); b = find(b);
        if (a == b) return a;
        if (sz[a] < sz[b]) swap(a,b);
        p[b] = a;
        sz[a] += sz[b];
        return a;
    }
};

int main(){
    int N, M;
    cin >> N >> M;

    vector<int> U(M+1), V(M+1);
    for(int i = 1; i <= M; ++i)
        cin >> U[i] >> V[i];

    int Q;
    cin >> Q;
    vector<int> X(Q);
    for(int i = 0; i < Q; ++i)
        cin >> X[i];

    DSU dsu(N);
    vector<unordered_set<int>> adj(N+1);
    for(int i = 1; i <= M; ++i){
        adj[U[i]].insert(V[i]);
        adj[V[i]].insert(U[i]);
    }

    long long edges = M;
    for(int i = 0; i < Q; ++i){
        int idx = X[i];
        int u = dsu.find(U[idx]);
        int v = dsu.find(V[idx]);
        if(u != v && adj[u].count(v)){
            --edges;
            if(adj[u].size() < adj[v].size()) swap(u,v);
            int root = dsu.unite(u, v);
            int other = (root == u ? v : u);
            for(int w: adj[other]){
                if(w == root) continue;
                if(adj[root].count(w)) --edges;
                adj[root].insert(w);
                adj[w].erase(other);
                adj[w].insert(root);
            }
            adj[other].clear();
        }
        cout << edges << "\n";
    }

    return 0;
}

提出情報

提出日時
問題 F - Contraction
ユーザ Naman____17
言語 C++ 17 (Clang 16.0.6)
得点 525
コード長 1575 Byte
結果 AC
実行時間 608 ms
メモリ 85916 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 525 / 525
結果
AC × 1
AC × 45
セット名 テストケース
Sample example_00.txt
All example_00.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, hand_12.txt, hand_13.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
ケース名 結果 実行時間 メモリ
example_00.txt AC 1 ms 3460 KiB
hand_00.txt AC 440 ms 81208 KiB
hand_01.txt AC 443 ms 81184 KiB
hand_02.txt AC 370 ms 79668 KiB
hand_03.txt AC 279 ms 79580 KiB
hand_04.txt AC 204 ms 27420 KiB
hand_05.txt AC 43 ms 4124 KiB
hand_06.txt AC 352 ms 85916 KiB
hand_07.txt AC 51 ms 23128 KiB
hand_08.txt AC 289 ms 77000 KiB
hand_09.txt AC 510 ms 79248 KiB
hand_10.txt AC 506 ms 81156 KiB
hand_11.txt AC 519 ms 83748 KiB
hand_12.txt AC 307 ms 53976 KiB
hand_13.txt AC 277 ms 33964 KiB
random_00.txt AC 210 ms 26280 KiB
random_01.txt AC 562 ms 71864 KiB
random_02.txt AC 305 ms 54464 KiB
random_03.txt AC 340 ms 75984 KiB
random_04.txt AC 601 ms 63176 KiB
random_05.txt AC 406 ms 64748 KiB
random_06.txt AC 198 ms 23320 KiB
random_07.txt AC 608 ms 79880 KiB
random_08.txt AC 284 ms 47168 KiB
random_09.txt AC 339 ms 76892 KiB
random_10.txt AC 421 ms 67872 KiB
random_11.txt AC 375 ms 51164 KiB
random_12.txt AC 205 ms 25880 KiB
random_13.txt AC 567 ms 74076 KiB
random_14.txt AC 330 ms 50320 KiB
random_15.txt AC 354 ms 75444 KiB
random_16.txt AC 572 ms 60460 KiB
random_17.txt AC 383 ms 54932 KiB
random_18.txt AC 232 ms 26808 KiB
random_19.txt AC 604 ms 79960 KiB
random_20.txt AC 351 ms 48712 KiB
random_21.txt AC 357 ms 76020 KiB
random_22.txt AC 489 ms 73652 KiB
random_23.txt AC 298 ms 46676 KiB
random_24.txt AC 198 ms 23420 KiB
random_25.txt AC 565 ms 73852 KiB
random_26.txt AC 344 ms 50892 KiB
random_27.txt AC 355 ms 75640 KiB
random_28.txt AC 446 ms 76580 KiB
random_29.txt AC 379 ms 49548 KiB