結果

問題 No.2148 ひとりUNO
ユーザー rlangevinrlangevin
提出日時 2023-02-09 08:59:13
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,820 bytes
コンパイル時間 305 ms
コンパイル使用メモリ 87,248 KB
実行使用メモリ 86,532 KB
最終ジャッジ日時 2023-09-20 19:17:22
合計ジャッジ時間 12,566 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import sys
readline = sys.stdin.readline

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]

from bisect import *
from copy import deepcopy
def compress(lst):
    """
    B: lstを座圧したリスト
    D: 元の値からindexを取得する辞書
    """
    B = []
    D = dict()
    vals = deepcopy(lst)
    vals = list(set(vals))
    vals.sort()
    for i in range(len(lst)):
        ind = bisect_left(vals, lst[i])
        B.append(ind)
    for i in range(len(B)):
        D[lst[i]] = B[i]
    return B, D

def f(c):
    if c == "R":
        return 0
    elif c == "G":
        return 1
    else:
        return 2



T = int(readline())
for _ in range(T):
    N = int(readline())
    C, D = [0] * N, [0] * N
    for i in range(N):
        C[i], D[i] = readline().split()
    D, _ = compress(D)
    C = list(map(f, C))
    U = UnionFind(3 * N)
    for i in range(N):
        U.union(i, N + C[i])
        U.union(i, N + 3 + D[i])
    
    S = set()
    for i in range(N):
        S.add(U.find(i))
    print("YES") if len(S) == 1 else print("NO")
0