BOJ - 4195 - 친구 네트워크

Updated:

import sys

def find(x):
    if(parent[x] == x): return x

    parent[x] = find(parent[x])
    return parent[x]

def unionx(x, y):
    rootX = find(x)
    rootY = find(y)

    if not rootX == rootY: parent[rootY] = rootX

def unionx(x, y):
    rootX = find(x)
    rootY = find(y)

    if not rootX == rootY:
        parent[rootY] = rootX
        number[rootX] += number[rootY]
    print(number[rootX])


def check(x, y):
    if find(x) == find(y): return True
    else: return False

N = int(input())
for _ in range(N):
    M = int(input())
    parent, number = {}, {}

    for _ in range(M):
        x, y = map(str, sys.stdin.readline().split())
        if x not in parent:
            parent[x] = x
            number[x] = 1
        if y not in parent:
            parent[y] = y
            number[y] = 1

        unionx(x, y)

https://www.acmicpc.net/problem/4195

Categories:

Updated:

Leave a comment