スパークリング黒ココア/RWS 2022 4. Find a RedSpica

Created Thu, 29 Dec 2022 Modified Tue, 26 Sep 2023 09:13:30 +0000

問題

Find a RedSpica

お気持ち

  • いっきゅう数より簡単なんですよ…(困惑)
  • UnionFindでグループ分けして、グループ毎に、「一番明るい星の色=R?」を確認していく、だけ。
    • UnionFindのgroup_count、おとなしくNをunionするたびにデクリメントするのが正解ですよねぇ、多分。

ソースコード

N = int(input())
b_L = list(map(int, input().split()))
c_L = list(map(str, input().split()))

uf = UnionFind(N)
M = int(input())

for i in range(M):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    uf.union(x, y)
ans = 0
for k, v in uf.all_group_members().items():

    max_color = 'Z'
    max_score = 0
    for idx in v:
        if b_L[idx] > max_score:
            max_color = c_L[idx]
            max_score = b_L[idx]
    if max_color == 'R':
        ans += 1
print(ans)