結果

問題 No.2148 ひとりUNO
ユーザー roarisroaris
提出日時 2022-12-05 06:00:57
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,510 bytes
コンパイル時間 230 ms
コンパイル使用メモリ 82,516 KB
実行使用メモリ 78,192 KB
最終ジャッジ日時 2024-04-20 09:08:42
合計ジャッジ時間 7,106 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
54,668 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 176 ms
77,084 KB
testcase_17 AC 173 ms
77,584 KB
testcase_18 AC 174 ms
77,296 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 174 ms
77,312 KB
testcase_22 AC 171 ms
77,252 KB
testcase_23 AC 170 ms
77,188 KB
testcase_24 AC 168 ms
77,832 KB
testcase_25 AC 169 ms
77,060 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 42 ms
53,872 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 = []
    uf = Unionfind(N+3)
    
    for _ in range(N):
        C, D = input().split()
        D = int(D)
        CD.append((C, D))
        uf.unite(f(C), 2+D)
    
    cnt = [0]*(N+3)
    
    for C, D in CD:
        cnt[uf.root(f(C))] += 1
    
    if max(cnt)==N:
        print('YES')
    else:
        print('NO')

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