結果

問題 No.2308 [Cherry 5th Tune B] もしかして、真?
ユーザー lloyzlloyz
提出日時 2023-05-21 00:20:46
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,861 bytes
コンパイル時間 596 ms
コンパイル使用メモリ 87,196 KB
実行使用メモリ 143,724 KB
最終ジャッジ日時 2023-08-23 10:35:52
合計ジャッジ時間 30,335 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,432 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 WA -
testcase_17 AC 887 ms
132,472 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 883 ms
132,084 KB
testcase_21 AC 690 ms
131,452 KB
testcase_22 WA -
testcase_23 AC 683 ms
130,636 KB
testcase_24 AC 685 ms
130,544 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 696 ms
131,556 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 683 ms
130,584 KB
testcase_31 AC 255 ms
133,228 KB
testcase_32 AC 266 ms
133,164 KB
testcase_33 AC 255 ms
133,188 KB
testcase_34 AC 298 ms
128,952 KB
testcase_35 AC 288 ms
129,040 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        '''
        UnionFindクラス。nは要素数を表す。
        '''
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        '''
        要素xを含む集合の親を見つける関数。
        '''
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        '''
        要素xを含む集合と要素yを含む集合を合体する関数。
        基本的には、要素数が多い集合に統合される。
        要素数が同じときは要素yを含む集合に統合される。
        '''
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        '''
        要素xを含む集合の要素数を出す関数。
        '''
        return -self.parents[self.find(x)]

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

for _ in range(int(input())):
    n = int(input())
    P = list(input().split())
    OP = list(input().split())
    X = list(map(int, input().split()))
    R = [i for i in range(n)]
    UF = UnionFind(n)
    for i in range(n - 1):
        idx = X[i] - 1
        ri = UF.find(idx)
        i = R[ri]
        op = OP[i]
        rj = UF.find(i + 1)
        j = R[rj]
        s, t = P[i], P[j]
        if op == 'and':
            if s == 'True' and t == 'True':
                res = 'True'
            else:
                res = 'False'
        elif op == 'or':
            if s == 'True' or t == 'True':
                res = 'True'
            else:
                res = 'False'
        elif op == 'xor':
            if s != t:
                res = 'True'
            else:
                res = 'False'
        elif op == 'imp':
            if s == 'True':
                res = t
            else:
                res = 'True'
        UF.union(ri, rj)
        r = UF.find(ri)
        R[r] = max(i, j)
        P[R[r]] = res
    r = UF.find(0)
    print(P[R[r]])
0