結果

問題 No.2148 ひとりUNO
ユーザー tktk_snsntktk_snsn
提出日時 2022-12-05 03:55:51
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,643 bytes
コンパイル時間 1,406 ms
コンパイル使用メモリ 142,696 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 07:52:00
合計ジャッジ時間 3,018 ms
ジャッジサーバーID
(参考情報)
judge2 / 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 12 ms
5,376 KB
testcase_17 AC 12 ms
5,376 KB
testcase_18 AC 11 ms
5,376 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 11 ms
5,376 KB
testcase_22 AC 11 ms
5,376 KB
testcase_23 AC 12 ms
5,376 KB
testcase_24 AC 12 ms
5,376 KB
testcase_25 AC 11 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 <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <iomanip>
#include <numeric>
#include <string>
#include <bitset>
#include <assert.h>
#include <functional>
#include <random>

// #define _GLIBCXX_DEBUG
// #define _LIBCPP_DEBUG 0
#define _GLIBCXX_DEQUE_BUF_SIZE 64

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define revrep(i, n) for(int i = (int)(n - 1); i >= 0; --i)
#define range(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrange(i, s, t) for(int i = (int)(t - 1); i >= (int)(s); --i)
#define srange(i, s, t, step) for(int i = (int)(s); i < (int)(t); i+=int(step))
#define popcnt(x) __builtin_popcountll(x)
#define hayai std::ios::sync_with_stdio(false);std::cin.tie(nullptr)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
template<class T> inline bool chmax(T& a, T b){ if (a < b){ a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b){ if (a > b){ a = b; return true; } return false; }
inline bool inside(int x, int y, int h, int w) { return (x >= 0 && x < h && y >= 0 && y < w); }
constexpr pair<int, int> dydx4[] = { {0, 1}, {-1, 0}, {0, -1}, {1, 0}, };   //RULD
constexpr pair<int, int> dydx8[] = { {0, 1},{-1, 1},{-1, 0},{-1, -1},{0, -1},{1, -1},{1, 0},{1, 1}, };


struct union_find{
  public:
    union_find(int size): _n(size), root(size, -1) {}

    int find(int x){
        if (root[x] < 0) return x;
        root[x] = find(root[x]);
        return root[x];
    }
    bool same(int x, int y){
        return find(x) == find(y);
    }
    bool merge(int x, int y){
        x = find(x); y = find(y);
        if (x == y) return false;
        if (-root[x] < -root[y]) swap(x, y);
        root[x] += root[y];
        root[y] = x;
        return true;
    }
    int size(int x){
        return -root[find(x)];
    }
  private:
    int _n;
    vector<int> root;
};


void solve() {
    int n;
    cin >> n;
    union_find uf(26 + n + n);
    rep(i, n) {
        char c;
        int d;
        cin >> c >> d;
        d--;
        uf.merge(c - 'A', 26 + n + i);
        uf.merge(26 + d, 26 + n + i);
    }
    int leader = uf.find(26 + n);
    rep(i, n) {
        int l = uf.find(26 + n + i);
        if(l != leader) {
            cout << "NO" << "\n";
            return;
        }
    }
    cout << "YES" << "\n";
}


int main() {
    hayai;
    int t;
    cin >> t;
    rep(i, t) solve();
}
0