結果

問題 No.2202 贅沢てりたまチキン
ユーザー Manuel1024Manuel1024
提出日時 2023-08-17 17:37:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 124 ms / 2,000 ms
コード長 971 bytes
コンパイル時間 802 ms
コンパイル使用メモリ 73,636 KB
実行使用メモリ 4,908 KB
最終ジャッジ日時 2023-08-17 17:37:58
合計ジャッジ時間 5,507 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,704 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 112 ms
4,716 KB
testcase_15 AC 113 ms
4,772 KB
testcase_16 AC 89 ms
4,908 KB
testcase_17 AC 89 ms
4,632 KB
testcase_18 AC 43 ms
4,376 KB
testcase_19 AC 57 ms
4,776 KB
testcase_20 AC 114 ms
4,704 KB
testcase_21 AC 115 ms
4,772 KB
testcase_22 AC 75 ms
4,380 KB
testcase_23 AC 93 ms
4,376 KB
testcase_24 AC 83 ms
4,380 KB
testcase_25 AC 124 ms
4,636 KB
testcase_26 AC 113 ms
4,572 KB
testcase_27 AC 113 ms
4,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct UnionFind{
    vector<int> p;
    UnionFind(int n): p(vector<int>(n, -1)){}

    int root(int x){
        if(p[x] < 0) return x;
        else return p[x] = root(p[x]);
    }

    int size(int x){
        return -p[root(x)];
    }
    
    bool same(int x, int y){
        return root(x) == root(y);
    }

    void unite(int x, int y){
        if(same(x, y)) return;
        x = root(x);
        y = root(y);
        if(size(x) < size(y)) swap(x, y);
        p[x] += p[y];
        p[y] = x;
    }
};

int main(){
    int n, m; cin >> n >> m;
    UnionFind uf(n*2);
    for(int i = 0; i < m; i++){
        int a, b; cin >> a >> b;
        a--; b--;
        uf.unite(a, n+b);
        uf.unite(n+a, b);
    }

    for(int i = 0; i < n; i++){
        if(!uf.same(i, n+i)){
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    return 0;
}
0