結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー FplusFplusFFplusFplusF
提出日時 2023-08-12 13:43:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,611 bytes
コンパイル時間 2,123 ms
コンパイル使用メモリ 204,148 KB
実行使用メモリ 6,664 KB
最終ジャッジ日時 2024-04-30 03:54:50
合計ジャッジ時間 3,339 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 26 ms
6,016 KB
testcase_04 AC 7 ms
6,016 KB
testcase_05 AC 17 ms
5,376 KB
testcase_06 AC 17 ms
5,888 KB
testcase_07 AC 17 ms
5,376 KB
testcase_08 AC 18 ms
6,664 KB
testcase_09 AC 20 ms
5,376 KB
testcase_10 AC 24 ms
5,888 KB
testcase_11 AC 22 ms
5,628 KB
testcase_12 AC 15 ms
5,376 KB
testcase_13 AC 8 ms
5,376 KB
testcase_14 AC 13 ms
5,376 KB
testcase_15 AC 13 ms
5,376 KB
testcase_16 AC 24 ms
5,376 KB
testcase_17 AC 7 ms
5,376 KB
testcase_18 AC 13 ms
5,504 KB
testcase_19 AC 5 ms
5,376 KB
testcase_20 AC 14 ms
5,376 KB
testcase_21 AC 9 ms
5,376 KB
testcase_22 AC 9 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll,ll>;
using ld=long double;
const ll INF=(1ll<<60);
#define rep(i,n) for (ll i=0;i<(ll)(n);i++)
#define all(v) v.begin(),v.end()
template<class T> void chmin(T &a,T b){
    if(a>b){
        a=b;
    }
}
template<class T> void chmax(T &a,T b){
    if(a<b){
        a=b;
    }
}
struct union_find{
    int n;
    vector<int> par,sz;
    union_find(int x):par(x),sz(x,1){
        n=x;
        for(int i=0;i<n;i++){
            par[i]=i;
        }
    }
    int root(int x){
        if(par[x]==x) return x;
        return par[x]=root(par[x]);
    }
    void unite(int x,int y){
        int rx=root(x);
        int ry=root(y);
        if(rx==ry) return;
        if(sz[rx]<sz[ry]) swap(rx,ry);
        par[ry]=rx;
        sz[rx]+=sz[ry];
    }
    bool same(int x,int y){
        int rx=root(x);
        int ry=root(y);
        return rx==ry;
    }
    int size(int x){
        return sz[root(x)];
    }
    vector<int> all_size(){
        vector<int> cnt(n,0);
        for(int i=0;i<n;i++){
            cnt[root(i)]++;

        }
        vector<int> ret;
        for(int i=0;i<n;i++){
            if(0<cnt[i]) ret.push_back(cnt[i]);
        }
        return ret;
    }
};
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n,m;
    cin >> n >> m;
    union_find uf(2*n);
    while(m--){
        ll a,b;
        cin >> a >> b;
        a--;
        b--;
        uf.unite(a,b);
    }
    vector<int> v=uf.all_size();
    ll ans=n;
    for(auto &i:v) ans-=i/2;
    cout << ans << '\n';
}
0