結果

問題 No.1390 Get together
ユーザー chocoruskchocorusk
提出日時 2021-02-12 21:28:10
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 286 ms / 2,000 ms
コード長 1,496 bytes
コンパイル時間 1,238 ms
コンパイル使用メモリ 127,764 KB
最終ジャッジ日時 2025-01-18 17:59:49
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <stack>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
struct unionfind{
    int cnt;
    vector<int> par, sz;
    unionfind() {}
    unionfind(int n):par(n), sz(n, 1), cnt(n){
        for(int i=0; i<n; i++) par[i]=i;
    }
    int find(int x){
        if(par[x]==x) return x;
        return par[x]=find(par[x]);
    }
    void unite(int x, int y){
        x=find(x); y=find(y);
        if(x==y) return;
        cnt--;
        if(sz[x]>sz[y]) swap(x, y);
        par[x]=y;
        sz[y]+=sz[x];
    }
    bool same(int x, int y){
        return find(x)==find(y);
    }
    int size(int x){
        return sz[find(x)];
    }
};
int main()
{
    int n, m; cin>>n>>m;
    unionfind uf(m);
    vector<int> v[200020];
    for(int i=0; i<n; i++){
        int b, c; cin>>b>>c;
        b--; c--;
        v[c].push_back(b);
    }
    for(int i=0; i<n; i++){
        if(!v[i].empty()){
            for(int j=0; j<(int)v[i].size()-1; j++){
                uf.unite(v[i][j], v[i][j+1]);
            }
        }
    }
    cout<<m-uf.cnt<<endl;
    return 0;
}
0