[BaekJoon] 10216번 : Count Circle Groups

Updated:

10216번 : Count Circle Groups


적ꡰ의 μ§„μ˜μ„ BFS 둜 νƒμƒ‰ν•˜λ©΄μ„œ 톡신이 λ˜λŠ” 경우 μ—°κ²°ν•΄ μ£Όλ©΄ λœλ‹€.

λ°©λ¬Έ ν•˜μ§€ μ•Šμ€ μ§„μ˜μ„ μ‹œμž‘μœΌλ‘œ BFS λ₯Ό μˆ˜ν–‰ν•˜κ³ , BFS λ₯Ό μˆ˜ν–‰ ν•œ νšŸμˆ˜κ°€ 그룹의 κ°œμˆ˜κ°€ λœλ‹€.

union-find λ₯Ό μ‚¬μš©ν•˜λ©΄ BFS 보닀 μ‹œκ°„μ„ 단좕 ν•  수 μžˆλ‹€.

ν•˜μ§€λ§Œ.. μ–΄λ–»κ²Œ μ μš©μ„ μ‹œμΌœμ•Ό 할지 아직 λͺ¨λ₯΄κ² λ‹€.

λ³΅μŠ΅μ„ ν•  λ•ŒλŠ” union-find λ₯Ό μ‚¬μš©ν•΄ 풀어봐야겠닀.

[21.03.11] 볡슡

union-find λ₯Ό μ μš©ν•΄ ν’€μ–΄λ³΄μ•˜λ‹€. 기쑴의 μ½”λ“œλŠ” 14000ms κ°€ 걸렸던 반면 μ§€κΈˆμ€ 2100ms κ°€ μ†Œμš”λœλ‹€.

μ²˜μŒμ—” κ°€λŠ₯ν•œ λͺ¨λ“  간선을 λ§Œλ“  λ’€ κ·Έ κ°„μ„ μ˜ 정보λ₯Ό 가지고 MST λ₯Ό λ§Œλ“œλŠ” λ°©μ‹μœΌλ‘œ ν–ˆλ‹€.

ν•˜μ§€λ§Œ μ‹œκ°„μ΄ˆκ³Όκ°€ λ°œμƒν–ˆκ³ , κ°€λŠ₯ν•œ λͺ¨λ“  간선을 λ§Œλ“€μ§€ μ•Šκ³  λ…Έλ“œ μ •λ³΄λ§Œμ„ 가지고 union-find λ₯Ό ν•΄μ•Όν–ˆλ‹€.

그런데 계속 μƒκ°ν•΄λ³΄λ‹ˆ μ• μ΄ˆμ— 간선을 λ§Œλ“€ μ΄μœ κ°€ μ—†μ—ˆλ‹€.

λ…Έλ“œμ˜ μ •λ³΄λ§ŒμœΌλ‘œ 두 λ…Έλ“œκ°€ 연결될지 μ•ˆλ μ§€λ₯Ό μ •ν•  수 μžˆμ—ˆκΈ° λ•Œλ¬Έμ΄λ‹€.

λ…Έλ“œ 정보λ₯Ό x_pos, y_pos, radius 둜 λ‚˜λˆ„μ–΄ μ €μž₯ν•˜λŠ” 방법과

nodes λ¦¬μŠ€νŠΈμ— ν•œ λ²ˆμ— μ €μž₯ν•œ λ’€ κ°€μ Έλ‹€ μ“°λŠ” 방법은 무렀 5000ms λ‚˜ 차이가 λ‚œλ‹€.


[21.03.11] 볡슡

import sys


def find_parent(parent, node):
    if parent[node] != node:
        parent[node] = find_parent(parent, parent[node])

    return parent[node]


def union_parent(parent, a, b):
    a = find_parent(parent, a)
    b = find_parent(parent, b)

    if a < b:
        parent[b] = a
    else:
        parent[a] = b


def solution():
    answer = node_cnt
    parent = [i for i in range(node_cnt)]

    for i in range(node_cnt):
        for j in range(i + 1, node_cnt):
            x_dif = x_pos[i] - x_pos[j]
            y_dif = y_pos[i] - y_pos[j]
            dist = radius[i] + radius[j]

            if (x_dif ** 2) + (y_dif ** 2) <= dist ** 2:
                if find_parent(parent, i) != find_parent(parent, j):
                    union_parent(parent, i, j)
                    answer -= 1

    return answer


if __name__ == "__main__":
    testcase = int(input())

    for _ in range(testcase):
        node_cnt = int(input())
        x_pos, y_pos, radius = [], [], []
        for _ in range(node_cnt):
            x, y, r = map(int, sys.stdin.readline().rsplit())
            x_pos.append(x)
            y_pos.append(y)
            radius.append(r)

        print(solution())

import sys
from collections import deque


def BFS(visit, start):
	queue = deque([nodes[start]])
	visit[start] = True

	while queue:
		x1, y1, r1 = queue.popleft()

		for idx, node in enumerate(nodes):
			if not visit[idx]:
				x2, y2, r2 = node
				dist = (x1 - x2) ** 2 + (y1 - y2) ** 2
				if dist <= (r1 + r2) ** 2:
					visit[idx] = True
					queue.append(node)


def solution():
	visit = [False] * node_cnt
	BFS_cnt = 0

	for idx, node in enumerate(nodes):
		if not visit[idx]:
			BFS(visit, idx)
			BFS_cnt += 1

	return BFS_cnt


if __name__ == "__main__":
	testcase = int(input())

	for _ in range(testcase):
		node_cnt = int(input())
		nodes = []
		for _ in range(node_cnt):
			x, y, r = map(int, sys.stdin.readline().rsplit())
			nodes.append([x, y, r])

		print(solution())



Categories:

Updated:

Leave a comment