結果

問題 No.2148 ひとりUNO
ユーザー roarisroaris
提出日時 2022-12-05 05:31:02
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,501 bytes
コンパイル時間 217 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 78,948 KB
最終ジャッジ日時 2024-04-20 08:47:11
合計ジャッジ時間 7,702 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,888 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 191 ms
78,672 KB
testcase_17 AC 195 ms
78,260 KB
testcase_18 AC 194 ms
78,384 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 192 ms
78,160 KB
testcase_22 AC 201 ms
78,476 KB
testcase_23 AC 204 ms
78,172 KB
testcase_24 AC 190 ms
78,656 KB
testcase_25 AC 186 ms
78,156 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 39 ms
54,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *

class Unionfind:
    def __init__(self, n):
        self.par = [-1]*n
        self.rank = [1]*n
    
    def root(self, x):
        r = x
        
        while not self.par[r]<0:
            r = self.par[r]
        
        t = x
        
        while t!=r:
            tmp = t
            t = self.par[t]
            self.par[tmp] = r
        
        return r
    
    def unite(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        
        if rx==ry:
            return
        
        if self.rank[rx]<=self.rank[ry]:
            self.par[ry] += self.par[rx]
            self.par[rx] = ry
            
            if self.rank[rx]==self.rank[ry]:
                self.rank[ry] += 1
        else:
            self.par[rx] += self.par[ry]
            self.par[ry] = rx
    
    def is_same(self, x, y):
        return self.root(x)==self.root(y)
    
    def count(self, x):
        return -self.par[self.root(x)]

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

def solve():
    N = int(input())
    CD = [tuple(input().split()) for _ in range(N)]
    uf = Unionfind(2*N+3)
    
    for i, (C, D) in enumerate(CD):
        uf.unite(N+3+i, N+f(C))
        uf.unite(N+3+i, int(D)-1)
    
    rs = set(uf.root(v) for v in range(N+3, 2*N+3))
    
    if len(rs)==1:
        print('YES')
    else:
        print('NO')

for _ in range(int(input())):
    solve()
0