๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โญ Problem_Solving/๋ฐฑ์ค€

[๋ฐฑ์ค€] 17471 ๊ฒŒ๋ฆฌ๋งจ๋”๋ง (Python/ํŒŒ์ด์ฌ)

by ํฌ์ŠคํŠธ์‰์ดํฌ 2022. 10. 14.

<๋ฌธ์ œ ๋งํฌ>

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

 

17471๋ฒˆ: ๊ฒŒ๋ฆฌ๋งจ๋”๋ง

์„ ๊ฑฐ๊ตฌ๋ฅผ [1, 4], [2, 3, 5, 6]์œผ๋กœ ๋‚˜๋ˆ„๋ฉด ๊ฐ ์„ ๊ฑฐ๊ตฌ์˜ ์ธ๊ตฌ๋Š” 9, 8์ด ๋œ๋‹ค. ์ธ๊ตฌ ์ฐจ์ด๋Š” 1์ด๊ณ , ์ด ๊ฐ’๋ณด๋‹ค ๋” ์ž‘์€ ๊ฐ’์œผ๋กœ ์„ ๊ฑฐ๊ตฌ๋ฅผ ๋‚˜๋ˆŒ ์ˆ˜๋Š” ์—†๋‹ค.

www.acmicpc.net

 

<ํ’€์ด ์ „๋žต>

1. ๋ถ€๋ถ„์ง‘ํ•ฉ / ์กฐํ•ฉ์„ ํ†ตํ•ด์„œ ๋‘ ์„ ๊ฑฐ๊ตฌ๋ฅผ ๊ตฌํ•œ ๋‹ค์Œ, ํ•ด๋‹น ์„ ๊ฑฐ๊ตฌ ๋‚ด์˜ ์ธ๊ตฌ์ˆ˜์™€ ๊ตฌ์—ญ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ๋ชจ๋“  ๊ตฌ์—ญ์ด ํฌํ•จ๋˜์—ˆ์„ ๊ฒฝ์šฐ์—๋งŒ ์ •๋‹ต์„ ๊ฐฑ์‹ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

2. ์กฐํ•ฉ์„ ๊ตฌํ•  ๋•Œ ์–ด์ฐจํ”ผ ์กฐํ•ฉ์— ํฌํ•จ๋˜์ง€ ๋ชปํ•œ ๊ตฌ์—ญ๋“ค์ด ๋‹ค๋ฅธ ์„ ๊ฑฐ๊ตฌ์— ํฌํ•จ๋˜๊ธฐ ๋•Œ๋ฌธ์— N//2๊นŒ์ง€๋งŒ ์กฐํ•ฉ์„ ๊ตฌํ•ด๋„ ๋œ๋‹ค.

3. ๊ทธ๋Ÿฌ๋‚˜ N๊นŒ์ง€ ๋ชจ๋“  ์กฐํ•ฉ์„ ๊ตฌํ•ด๋„ ๋ณ„ ๋ฌธ์ œ ์—†์ด ์‹œ๊ฐ„ ๋‚ด์— ํ†ต๊ณผ ๋œ๋‹ค.

 

<์ •๋‹ต ์ฝ”๋“œ>

from collections import deque
from itertools import combinations
import sys, os, io, atexit

input = lambda: sys.stdin.readline().rstrip('\r\n')
stdout = io.BytesIO()
sys.stdout.write = lambda s: stdout.write(s.encode("ascii"))
atexit.register(lambda: os.write(1, stdout.getvalue()))

# 221009 17471 ๊ฒŒ๋ฆฌ๋งจ๋”๋ง

# ์ •๋‹ต์ฝ”๋“œ


# bfs ํƒ์ƒ‰์„ ํ†ตํ•ด์„œ ์„ ๊ฑฐ๊ตฌ์— ํฌํ•จ๋œ ๊ตฌ์—ญ ์ˆ˜๋ž‘ ์ธ๊ตฌ ์ˆ˜ ๊ณ„์‚ฐ
def bfs(group):
    # ์„ ๊ฑฐ๊ตฌ ๋‚ด์˜ ์ž„์˜์˜ ํ•œ ๊ตฌ์—ญ์„ ์ถœ๋ฐœ ์ง€์ ์œผ๋กœ ์ง€์ •
    start = group[0]
    visited[start] = 1

    queue = deque()
    queue.append(start)

    # cnt: ์„ ๊ฑฐ๊ตฌ ๋‚ด์— ํฌํ•จ๋œ ๊ตฌ์—ญ ๊ฐœ์ˆ˜
    cnt = 1
    # group_pop: ์„ ๊ฑฐ๊ตฌ์˜ ์ด ์ธ๊ตฌ์ˆ˜ ํ•ฉ๊ณ„
    group_pop = population[start]
    while queue:
        current = queue.popleft()
        # ์ธ๊ตฌ ์ˆ˜๋ž‘ ๊ตฌ์—ญ ๊ฐœ์ˆ˜ ์„ธ์ฃผ๋ฉด์„œ ์ธ์ ‘ ๊ตฌ์—ญ ํƒ์ƒ‰ 
        for next in graph[current]:
            if next in group and not visited[next]:
                cnt += 1
                group_pop += population[next]

                visited[next] = 1
                queue.append(next)

    # ๊ตฌ์—ญ ๊ฐœ์ˆ˜, ์ธ๊ตฌ์ˆ˜ ๋ฐ˜ํ™˜
    return cnt, group_pop


# N: ๊ตฌ์—ญ์˜ ๊ฐœ์ˆ˜, poplulation: ๊ตฌ์—ญ ์ธ๊ตฌ ์ •๋ณด, numbers: ๊ตฌ์—ญ ๋ฒˆํ˜ธ
N = int(input())
population = [0] + list(map(int, input().split()))
numbers = [i for i in range(1, N + 1)]

# graph: ๊ตฌ์—ญ ์—ฐ๊ฒฐ ๊ด€๊ณ„
graph = [[] for _ in range(N + 1)]
for i in range(1, N + 1):
    temp = list(map(int, input().split()))
    for j in range(1, temp[0] + 1):
        graph[i].append(temp[j])

# ans: ์ธ๊ตฌ ์ฐจ์ด ์ตœ์†Ÿ๊ฐ’(์ถฉ๋ถ„ํžˆ ํฐ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”)
ans = float('INF')
# ์กฐํ•ฉ์„ ์ ˆ๋ฐ˜๊นŒ์ง€๋งŒ ๊ณ„์‚ฐํ•œ๋‹ค.
for i in range(1, N//2 + 1):
    for comb in combinations(numbers, i):
        # group_1, group_2: ์„ ๊ฑฐ๊ตฌ 1, ์„ ๊ฑฐ๊ตฌ 2
        group_1 = comb
        group_2 = list(set(numbers) - set(comb))

        # visited: ๋ฐฉ๋ฌธ ์—ฌ๋ถ€ ํ‘œ์‹œ ๋ฐฐ์—ด
        visited = [0 for _ in range(N + 1)]

        # bfs ํƒ์ƒ‰์„ ํ†ตํ•ด์„œ ๊ฐ ์„ ๊ฑฐ๊ตฌ์— ํฌํ•จ๋œ ๊ตฌ์—ญ ์ˆ˜๋ž‘ ์ธ๊ตฌ ์ˆ˜ ๊ณ„์‚ฐ
        cnt_1, group_pop_1 = bfs(group_1)
        cnt_2, group_pop_2 = bfs(group_2)

        # ๋ชจ๋“  ๊ตฌ์—ญ์ด ์„ ๊ฑฐ๊ตฌ์— ํฌํ•จ๋˜์–ด์žˆ์„ ๊ฒฝ์šฐ
        if cnt_1 + cnt_2 == N:
            # ์ตœ์†Ÿ๊ฐ’ ๊ฐฑ์‹ 
            ans = min(ans, abs(group_pop_1 - group_pop_2))

# ์„ ๊ฑฐ๊ตฌ๋ฅผ ๋‚˜๋ˆŒ ์ˆ˜ ์—†์œผ๋ฉด -1 ์ถœ๋ ฅ
if ans == float('INf'):
    print(-1)
# ์ •๋‹ต ์ถœ๋ ฅ
else:
    print(ans)

๋Œ“๊ธ€