結果

問題 No.1390 Get together
ユーザー rogi52rogi52
提出日時 2022-10-04 20:01:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 194 ms / 2,000 ms
コード長 1,155 bytes
コンパイル時間 2,314 ms
コンパイル使用メモリ 212,028 KB
実行使用メモリ 34,468 KB
最終ジャッジ日時 2023-08-28 20:40:40
合計ジャッジ時間 7,978 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 134 ms
27,276 KB
testcase_17 AC 51 ms
15,472 KB
testcase_18 AC 194 ms
34,468 KB
testcase_19 AC 131 ms
22,012 KB
testcase_20 AC 94 ms
18,252 KB
testcase_21 AC 97 ms
18,524 KB
testcase_22 AC 158 ms
27,352 KB
testcase_23 AC 153 ms
27,380 KB
testcase_24 AC 161 ms
27,304 KB
testcase_25 AC 119 ms
21,488 KB
testcase_26 AC 118 ms
21,468 KB
testcase_27 AC 123 ms
21,484 KB
testcase_28 AC 121 ms
21,476 KB
testcase_29 AC 117 ms
21,532 KB
testcase_30 AC 124 ms
21,532 KB
testcase_31 AC 123 ms
21,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

class union_find {
  public:
    union_find(int n) : data(n, -1) {}
    int unite(int x, int y) {
        x = root(x), y = root(y);
        if(x != y) {
            if(size(x) < size(y)) swap(x, y);
            data[x] += data[y];
            return data[y] = x;
        }
        return -1;
    }
    int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
    int size(int x) { return -data[root(x)]; }
    bool same(int x, int y) { return root(x) == root(y); }

  private:
    vector<int> data;
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int N,M; cin >> N >> M;
    union_find uf(N);
    vector<pair<int,int>> bc(N);
    vector<int> box(M, -1);
    for(auto &[b, c] : bc) {
        cin >> b >> c; b--; c--;
        if(box[b] != -1) uf.unite(box[b], c);
        box[b] = c;
    }

    vector<set<int>> pos(N);
    for(auto &[b, c] : bc) {
        c = uf.root(c);
        pos[c].insert(b);
    }

    int ans = 0;
    for(auto st : pos) ans += max(0, int(st.size()) - 1);
    cout << ans << endl;
}
0