ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 23040 |
문제명 | 누텔라 트리 (Easy) |
레벨 | 골드 3 |
분류 |
Disjoint Set |
시간복잡도 | O(n*α(n)) |
인풋사이즈 | n<=100,000 |
사용한 언어 | Python |
제출기록 | 40788KB / 312ms |
최고기록 | 312ms |
해결날짜 | 2021/10/18 |
"""Solution code for "BOJ 23040. 누텔라 트리 (Easy)".
- Problem link: https://www.acmicpc.net/problem/23040
- Solution link: http://www.teferi.net/ps/problems/boj/23040
Tags: [DisjointSet]
"""
import sys
from teflib import disjointset
def main():
N = int(sys.stdin.readline())
edges = [sys.stdin.readline() for _ in range(N - 1)]
color = sys.stdin.readline()
dsu = disjointset.DisjointSet(N)
red_groups = []
for edge in edges:
u, v = [int(x) for x in edge.split()]
if color[u - 1] == color[v - 1] == 'R':
dsu.union(u - 1, v - 1)
elif color[u - 1] == 'R':
red_groups.append(u - 1)
elif color[v - 1] == 'R':
red_groups.append(v - 1)
print(sum(dsu.size(x) for x in red_groups))
if __name__ == '__main__':
main()