結果

問題 No.1390 Get together
ユーザー auauaauaua
提出日時 2021-02-12 21:33:40
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 1,666 bytes
コンパイル時間 1,684 ms
コンパイル使用メモリ 171,188 KB
実行使用メモリ 15,180 KB
最終ジャッジ日時 2023-09-27 03:06:15
合計ジャッジ時間 5,845 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 115 ms
12,468 KB
testcase_17 AC 158 ms
15,096 KB
testcase_18 AC 123 ms
12,476 KB
testcase_19 AC 162 ms
15,112 KB
testcase_20 AC 160 ms
15,040 KB
testcase_21 AC 164 ms
15,120 KB
testcase_22 AC 140 ms
14,000 KB
testcase_23 AC 145 ms
14,068 KB
testcase_24 AC 146 ms
14,136 KB
testcase_25 AC 163 ms
15,012 KB
testcase_26 AC 164 ms
15,176 KB
testcase_27 AC 163 ms
15,180 KB
testcase_28 AC 166 ms
15,068 KB
testcase_29 AC 164 ms
15,032 KB
testcase_30 AC 164 ms
15,056 KB
testcase_31 AC 160 ms
15,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll inf=INF*INF;
const ll mod=998244353;
const ll MAX=140010;

//Union-Find木

struct UnionFind {
    vector< int > data;

    UnionFind() = default;

    explicit UnionFind(size_t sz) : data(sz, -1) {}

    bool unite(int x, int y) {
        x = find(x), y = find(y);
        if(x == y) return false;
        if(data[x] > data[y]) swap(x, y);
        data[x] += data[y];
        data[y] = x;
        return true;
    }

    int find(int k) {
        if(data[k] < 0) return (k);
        return data[k] = find(data[k]);
    }

    int size(int k) {
        return -data[find(k)];
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }
};

signed main(){
    ll n,m;cin>>n>>m;
    UnionFind uf(m);
    vector<vector<ll> >box(n);
    rep(i,n){
        ll b,c;cin>>b>>c;
        c--;
        b--;
        box[c].pb(b);
    }
    rep(i,n){
        REP(j,1,box[i].size()){
            uf.unite(box[i][j],box[i][j-1]);
        }
    }
    vector<ll>used(m);
    ll cnt=0;
    rep(i,m){
        if(used[uf.find(i)])continue;
        used[uf.find(i)]=1;
        cnt++;
    }
    cout<<m-cnt<<endl;
}
0