結果

問題 No.2148 ひとりUNO
ユーザー ぷらぷら
提出日時 2022-12-12 00:32:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,418 bytes
コンパイル時間 2,171 ms
コンパイル使用メモリ 205,728 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-23 18:48:20
合計ジャッジ時間 3,891 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 9 ms
5,376 KB
testcase_17 AC 9 ms
5,376 KB
testcase_18 AC 9 ms
5,376 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 10 ms
5,376 KB
testcase_22 AC 9 ms
5,376 KB
testcase_23 AC 9 ms
5,376 KB
testcase_24 AC 9 ms
5,376 KB
testcase_25 AC 9 ms
5,376 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct UnionFind {
    vector<int> par;
    vector<int> size;
    UnionFind(int n) {
        par.resize(n);
        size.resize(n,1);
        for(int i = 0; i < n; i++) {
            par[i] = i;
        }
    }
    int find(int x) {
        if(par[x] == x) {
            return x;
        }
        return par[x] = find(par[x]);
    }
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    int consize(int x) {
        return size[find(x)];
    }
    bool unite(int x, int y) {
        x = find(x);
        y = find(y);
        if(x == y) {
            return false;
        }
        if(size[x] > size[y]) {
            swap(x,y);
        }
        par[x] = y;
        size[y] += size[x];
        return true;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while(t--) {
        int N;
        cin >> N;
        vector<char>C(N);
        vector<int>D(N);
        UnionFind uf(2*N+3);
        for(int i = 0; i < N; i++) {
            cin >> C[i] >> D[i];
            D[i]--;
            for(int j = 0; j < 3; j++) {
                if("RGB"[j] == C[i]) uf.unite(N+j,i);
            }
            uf.unite(N+3+D[i],i);
        }
        set<int>st;
        for(int i = 0; i < N; i++) {
            st.insert(uf.find(i));
        }
        cout << ((st.size() == 1)?"YES":"NO") << "\n";
    }
}
0