結果

問題 No.2780 The Bottle Imp
ユーザー manukemanuke
提出日時 2024-06-08 02:04:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,519 bytes
コンパイル時間 1,529 ms
コンパイル使用メモリ 169,004 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-08 10:40:43
合計ジャッジ時間 3,610 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 28 ms
5,376 KB
testcase_08 AC 28 ms
5,376 KB
testcase_09 AC 28 ms
5,376 KB
testcase_10 AC 27 ms
5,376 KB
testcase_11 AC 27 ms
5,376 KB
testcase_12 AC 42 ms
5,376 KB
testcase_13 AC 42 ms
5,376 KB
testcase_14 AC 24 ms
5,376 KB
testcase_15 AC 25 ms
5,376 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 24 ms
5,376 KB
testcase_19 AC 24 ms
5,376 KB
testcase_20 AC 24 ms
5,376 KB
testcase_21 WA -
testcase_22 AC 12 ms
5,376 KB
testcase_23 AC 20 ms
5,376 KB
testcase_24 AC 23 ms
5,376 KB
testcase_25 AC 35 ms
5,376 KB
testcase_26 AC 23 ms
5,376 KB
testcase_27 AC 22 ms
5,376 KB
testcase_28 AC 21 ms
5,376 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 36 ms
5,376 KB
testcase_32 AC 17 ms
5,376 KB
testcase_33 AC 39 ms
5,376 KB
testcase_34 AC 38 ms
5,376 KB
testcase_35 AC 14 ms
5,376 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 14 ms
5,376 KB
testcase_39 AC 34 ms
5,376 KB
testcase_40 AC 34 ms
5,376 KB
testcase_41 WA -
testcase_42 AC 1 ms
5,376 KB
testcase_43 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
    vector<int> par, siz;

    // 構造体の初期化
    UnionFind(int n) : par(n), siz(n, 1) {
        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]); // 経路圧縮
    }

    // x と y が同じグループに属するか (= 根が一致するか)
    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    // x を含むグループと y を含むグループを併合する
    bool unite(int x, int y) {
        int rx = root(x), ry = root(y); // x 側と y 側の根を取得する
        if (rx == ry) return false; // すでに同じグループのときは何もしない
        // union by size
        if (siz[rx] < siz[ry]) swap(rx, ry); // ry 側の siz が小さくなるようにする
        par[ry] = rx; // ry を rx の子とする
        siz[rx] += siz[ry]; // rx 側の siz を調整する
        return true;
    }

    // x を含む根付き木のサイズを求める
    int size(int x) {
        return siz[root(x)];
    }
};

int main() {
    // 入力を受け取る
    int N;
    cin >> N;
    UnionFind uf(N);
    for(int i=0;i<N;i++){
        int m;
        cin >> m;
        for(int j=0;j<m;j++){
            int a;
            cin >> a;
            uf.unite(i,a-1);
        }
    }
    if(uf.size(0)==N) cout << "Yes" << endl;
    else cout << "No" << endl;
	return 0;
}
0