結果

問題 No.2072 Anatomy
ユーザー umimelumimel
提出日時 2022-09-16 21:56:19
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 1,522 bytes
コンパイル時間 1,470 ms
コンパイル使用メモリ 170,060 KB
実行使用メモリ 11,104 KB
最終ジャッジ日時 2023-08-23 15:21:09
合計ジャッジ時間 6,111 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 100 ms
9,780 KB
testcase_09 AC 109 ms
10,472 KB
testcase_10 AC 106 ms
8,724 KB
testcase_11 AC 111 ms
9,844 KB
testcase_12 AC 86 ms
8,320 KB
testcase_13 AC 114 ms
10,760 KB
testcase_14 AC 73 ms
7,524 KB
testcase_15 AC 68 ms
7,928 KB
testcase_16 AC 117 ms
10,632 KB
testcase_17 AC 113 ms
10,704 KB
testcase_18 AC 59 ms
6,992 KB
testcase_19 AC 119 ms
11,080 KB
testcase_20 AC 119 ms
10,884 KB
testcase_21 AC 115 ms
10,956 KB
testcase_22 AC 120 ms
10,980 KB
testcase_23 AC 117 ms
11,012 KB
testcase_24 AC 114 ms
10,980 KB
testcase_25 AC 119 ms
10,972 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 118 ms
10,840 KB
testcase_28 AC 116 ms
11,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second

const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll INF = 1LL << 60;
const ll MAX_N = 2e5;

struct UnionFind{
    vector<ll> parent;
    vector<ll> sizes;
    vector<ll> cnt;
    
    UnionFind(ll N) : parent(N), sizes(N, 1), cnt(N, 0){
        rep(i, N) parent[i] = i;
    }
    
    ll root(ll x){
        if (parent[x] == x) return x;
        return parent[x] = root(parent[x]);
    }
 
    void unite(ll x, ll y){
        ll rx = root(x);
        ll ry = root(y);
        if (rx == ry){
            cnt[rx]++;
            return;
        }
        if (sizes[rx] < sizes[ry]) swap(rx, ry);
        cnt[rx] = max(cnt[rx], cnt[ry])+1;
        sizes[rx] += sizes[ry];
        parent[ry] = rx;
    }
 
    bool same(ll x, ll y){
        ll rx = root(x);
        ll ry = root(y);
        return rx == ry;
    }
 
    ll size(ll x){
        return sizes[root(x)];
    }

    ll count(ll x){
        return cnt[root(x)];
    }
};

int main(){
    ll n, m;
    cin >> n >> m;

    vector<pll> e(m);
    rep(i, m){
        ll u, v;
        cin >> u >> v;
        u--; v--;
        e[i] = {u, v};
    }

    UnionFind UF(n);
    
    for(ll i=m-1; i>=0; i--){
        UF.unite(e[i].fi, e[i].se);
    }

    cout << UF.count(0) << endl;
}
0