結果

問題 No.1390 Get together
ユーザー shu8Creamshu8Cream
提出日時 2021-02-13 09:14:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 1,325 bytes
コンパイル時間 2,172 ms
コンパイル使用メモリ 211,116 KB
実行使用メモリ 14,212 KB
最終ジャッジ日時 2023-09-27 17:13:26
合計ジャッジ時間 6,722 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 53 ms
14,016 KB
testcase_17 AC 43 ms
14,140 KB
testcase_18 AC 63 ms
14,012 KB
testcase_19 AC 58 ms
13,884 KB
testcase_20 AC 53 ms
13,956 KB
testcase_21 AC 52 ms
14,148 KB
testcase_22 AC 60 ms
13,936 KB
testcase_23 AC 63 ms
14,132 KB
testcase_24 AC 62 ms
13,820 KB
testcase_25 AC 60 ms
13,796 KB
testcase_26 AC 58 ms
13,876 KB
testcase_27 AC 59 ms
14,136 KB
testcase_28 AC 59 ms
14,172 KB
testcase_29 AC 58 ms
14,212 KB
testcase_30 AC 59 ms
13,868 KB
testcase_31 AC 60 ms
14,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
*    author:  shu8Cream
*    created: 12.02.2021 21:19:22
**/

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i<(n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using P = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;

struct UnionFind{
    vector<int> par;
    vector<map<int, int>> mp;
    UnionFind(int n=0): par(n, -1), mp(n) {}
    int find(int x){
        if(par[x] < 0) return x;
        return par[x] = find(par[x]);
    }
    bool unite(int x, int y){
        x = find(x); y = find(y);
        if(x == y) return false;
        if(par[x] > par[y]) swap(x,y); //マージテク
        for(auto p : mp[y]){
            mp[x][p.first] += p.second;
        }
        mp[y] = map<int,int>(); //メモリの解放
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    bool same(int x, int y) {return find(x) == find(y);}
    int size(int x) {return -par[find(x)];}
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,m;
    cin >> n >> m;
    UnionFind uf(m);
    vi f(n,-1);
    int ans = 0;
    rep(i,n){
        int b,c;
        cin >> b >> c;
        --b; --c;
        if(f[c]>=0){
            if(!uf.same(b,f[c])) ans++, uf.unite(b,f[c]);
        }else f[c]=b;
    }
    cout << ans << endl;
}
0