
๐ป ๋ฌธ์ ์ ์

๋ค์ ์งํ์ฒ 2ํธ์ ๊ณผ ๊ฐ์ ํํ์ ๊ทธ๋ํ๊ฐ ์ฃผ์ด์ก์ ๋, ๊ฐ ๊ตฌ๊ฐ๊ณผ ์ํ ๊ตฌ๊ฐ ์ฌ์ด์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ์ด๋ค.
๐ก ์ ๊ทผ ๋ฐ ์ค๊ณ
๊ทธ๋ํ์์ ์ฌ์ดํด์ด ๋ฐ์ํ๋ ๊ตฌ๊ฐ์ ๊ตฌํ๊ณ ํด๋น ๊ตฌ๊ฐ์ ํฌํจ๋ ๋ ธ๋๋ค์ ๊ฑฐ๋ฆฌ๋ 0์ด ๋๋ค. ์ดํ ์ฌ์ดํด์ด ์๋ ๋ ธ๋์ ๋ํด์ ์ฌ์ดํด์ด ํฌํจ๋ ๋ ธ๋๊น์ง์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ฉด ๋๋ค.
๋ฌธ์ ์์ ๋ ธ๋์ ๊ฐ์ N๊ฐ, ๊ทธ๋ฆฌ๊ณ ๊ฐ์ ์ ์๋ N๊ฐ์ด๋ค. ๋ฐ๋ผ์ ํด๋น ๊ทธ๋ํ์๋ ๋ฐ๋์ ํ๋์ ์ฌ์ดํด์ด ์กด์ฌํ๋ค. DFS๋ก ํ์ ์์ ๋ ธ๋์ ํ์ฌ ๋ฐฉ๋ฌธ ๋ ธ๋๋ฅผ ๋น๊ตํ๋ฉฐ ๊ฐ๋ค๋ฉด, ์ฌ์ดํด์ด ๋ฐ์ํ๋ค๊ณ ํ ์ ์๋ค. ์ดํ ์ฌ์ดํด์ด ๋ฐ์ํ์ง ์๋ ๋ ธ๋์ ๋ํด์ BFS๋ฅผ ์ด์ฉํ์ฌ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ ์ ์์๊ฒ์ด๋ค.
โ๏ธ ์๊ณ ๋ฆฌ์ฆ ํ์ด
cycle = [False] * (N+1)
def dfs(node, start, cnt):
# ํ์ฌ ๋ฐฉ๋ฌธ ์ ๊ณผ ์์์ ์ด ๊ฐ๊ณ , ์ง๊ธ๊น์ง ๋ฐฉ๋ฌธํ ๋
ธ๋ ์๊ฐ 2๊ฐ ์ด์์ด๋ฉด
if node == start and cnt >= 2:
cycle[node] = True
return
visited[node] = True
for ix in graph[node]:
if not visited[ix]:
dfs(ix, start, cnt+1)
elif ix == start and cnt >= 2:
dfs(ix, start, cnt)
for i in range(1, N+1):
visited = [False] * (N+1)
dfs(i, i, 0)
๋ฐฉ๋ฌธ ๋ ธ๋์ ํ์ ์์๋ ธ๋, ๊ทธ๋ฆฌ๊ณ ์ง๊ธ๊น์ง ๋ฐฉ๋ฌธํ ๋ ธ๋์ ๊ฐ์ ์ ๋ณด๋ฅผ ํตํด ํ์์ ์์ํ๋ค. ๋ฐฉ๋ฌธํ์ง ์์ ๋ ธ๋๋ก ํ์์ ์ด์ด๋๊ฐ๋ ๋ฐฉ๋ฌธ ๋ ธ๋ ๊ฐ์๋ฅผ ์ฆ๊ฐ์์ผ์ฃผ๊ณ , ๋ค์ ๋ฐฉ๋ฌธ ์์ ์ธ ๋ ธ๋๊ฐ ์์์ ๊ณผ ์ผ์นํ๋ค๋ฉด ์ง๊ธ ์ ๋ณด ๊ทธ๋๋ก ํ์์ ์ด์ด๋๊ฐ๋ค. ์ต์ข ์ ์ผ๋ก ํ์ฌ ๋ฐฉ๋ฌธ ๋ ธ๋์ ์์ ๋ ธ๋๊ฐ ๊ฐ๊ณ , ๋ฐฉ๋ฌธํ ๋ ธ๋์ ๊ฐ์๊ฐ 2๊ฐ ์ด์์ด๋ผ๋ฉด ํด๋น ๋ ธ๋๋ ์ฌ์ดํด์ด ๋ฐ์ํ๋ค.
def bfs():
Q = deque()
for i in range(1, N+1):
if cycle[i]:
Q.append(i)
visited[i] = 0
while Q:
curr = Q.popleft()
for ix in graph[curr]:
if visited[ix] == -1:
visited[ix] = visited[curr]+1
Q.append(ix)
visited = [-1] * (N+1)
bfs()
์ดํ ์ฌ์ดํด์ด ์กด์ฌํ๋ ๋ ธ๋๋ก๋ถํฐ ์ฌ์ดํด์ด ์กด์ฌํ์ง ์๋ ๋ ธ๋๋ก BFS๋ฅผ ์ด์ด๊ฐ๋ฉฐ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ค. ์ต์ข ์ ์ผ๋ก visited๋ฆฌ์คํธ์ ๊ฑฐ๋ฆฌ ์ ๋ณด๊ฐ ์ ์ฅ๋ ๊ฒ์ด๋ค.
๐๏ธ ์ต์ข ์ ์ถ ์ฝ๋
# 16947 ์์ธ ์งํ์ฒ 2ํธ์
import sys
from collections import deque
sys.setrecursionlimit(10 ** 6)
def dfs(node, start, cnt):
# ํ์ฌ ๋ฐฉ๋ฌธ ์ ๊ณผ ์์์ ์ด ๊ฐ๊ณ , ์ง๊ธ๊น์ง ๋ฐฉ๋ฌธํ ๋
ธ๋ ์๊ฐ 2๊ฐ ์ด์์ด๋ฉด
if node == start and cnt >= 2:
cycle[node] = True
return
visited[node] = True
for ix in graph[node]:
if not visited[ix]:
dfs(ix, start, cnt+1)
elif ix == start and cnt >= 2:
dfs(ix, start, cnt)
def bfs():
Q = deque()
for i in range(1, N+1):
if cycle[i]:
Q.append(i)
visited[i] = 0
while Q:
curr = Q.popleft()
for ix in graph[curr]:
if visited[ix] == -1:
visited[ix] = visited[curr]+1
Q.append(ix)
N = int(input())
graph = [[] for _ in range(N+1)]
for _ in range(N):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
cycle = [False] * (N+1)
for i in range(1, N+1):
visited = [False] * (N+1)
dfs(i, i, 0)
visited = [-1] * (N+1)
bfs()
for i in range(1, N+1):
print(visited[i], end=' ')
โ ๋ ๋ค๋ฅธ ํ์ด
์ฌ์ดํด์ด ๋ฐ์ํ๋ ๊ตฌ๊ฐ์ ์ฐพ๋ ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ด ์๋ค. ๊ฐ ๋ ธ๋์ ์ฐจ์๋ฅผ ์ด์ฉํ ๋ฐฉ๋ฒ์ด๋ค.
- ๊ฐ ๋ ธ๋์ ์ฐจ์๋ฅผ ๊ตฌํ๋ค. ๋ฆฌํ ๋ ธ๋์ ์ฐจ์๋ 1์ด๋ค.
- ์ฐจ์๊ฐ 1์ธ ๋ ธ๋์ ๋ํด DFS๋ฅผ ์ด์ด๋๊ฐ๋ค. ๋ฆฌํ ๋ ธ๋๋ฅผ ์ ๊ฑฐํ๋ค.
- ๋ชจ๋ ๋ฆฌํ๋ ธ๋๋ฅผ ์ ๊ฑฐํ ํ์๋ ์ฐจ์๊ฐ 1๋ณด๋ค ํฐ ๋ ธ๋๋ค์ ์ฌ์ดํด์ด ๋ฐ์ํ๋ค.
์ฐจ์(degree) : ํ ๋ ธ๋์ ์กด์ฌํ๋ ๊ฐ์ ์ ๊ฐ์. ๋ฐ๋ผ์ ์ฐจ์๊ฐ 1์ด๋ฉด ํด๋น ๋ ธ๋๋ ๊ฐ์ ์ด ํ ๊ฐ ์กด์ฌ. ๋ฐ๋ผ์ leaf_node๊ฐ ๋ ๊ฒ์ด๊ณ , ์ฐจ์๊ฐ 2 ์ด์์ด๋ผ๋ฉด leaf_node๊ฐ ์๋๋ค. ๋ชจ๋ leaf_node๋ฅผ ์ ๊ฑฐํ ํ์๋ ๊ฐ์ ์ ๊ฐ์๊ฐ 2๊ฐ ์ด์์ด๋ผ๋ฉด, ํด๋น ๋ ธ๋๋ ์ฌ์ดํด์ ์กด์ฌํ๋ค๊ณ ๋ณผ ์ ์๋ค.
cycle = [True] * (N+1)
def find_cycle():
Q = deque()
for i in range(1, N+1):
if degree[i] == 1:
Q.append(i)
while Q:
curr = Q.popleft()
cycle[curr] = False
for next in graph[curr]:
degree[next] -= 1
if degree[next] == 1:
Q.append(next)
์ฐจ์๋ฅผ ์ด์ฉํด leaf_node๋ฅผ ์ ๊ฑฐํ๊ณ , ๋จ์์๋ ๋ ธ๋๋ค์ ์ฌ์ดํด์ ๊ตฌ์ฑํจ์ ์ด์ฉํ ํ์ด ๋ฐฉ๋ฒ์ด๋ค.
๐๏ธ ์ต์ข ์ ์ถ ์ฝ๋ (2)
# 16947 ์์ธ ์งํ์ฒ 2ํธ์
import sys
from collections import deque
sys.setrecursionlimit(10 ** 6)
def find_cycle():
Q = deque()
for i in range(1, N+1):
if degree[i] == 1:
Q.append(i)
while Q:
curr = Q.popleft()
cycle[curr] = False
for next in graph[curr]:
degree[next] -= 1
if degree[next] == 1:
Q.append(next)
def bfs():
Q = deque()
for i in range(1, N+1):
if cycle[i]:
Q.append(i)
visited[i] = 0
while Q:
curr = Q.popleft()
for ix in graph[curr]:
if visited[ix] == -1:
visited[ix] = visited[curr]+1
Q.append(ix)
N = int(input())
graph = [[] for _ in range(N+1)]
degree = [0] * (N+1)
for _ in range(N):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
degree[a] += 1
degree[b] += 1
cycle = [True] * (N+1)
find_cycle()
visited = [-1] * (N+1)
bfs()
for v in visited[1:]:
print(v, end=' ')
๐ญ ์ค๋์ ํ๊ณ
๊ทธ๋ํ์์ ์ฌ์ดํด์ ์ฐพ๋ ํ ํ์์ ํตํด ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ค๋ ์๊ฐ์ ํ์ผ๋, ์ฌ์ดํด์ ์ฐพ๋ ๋ฐฉ๋ฒ์ ๋ชฐ๋ผ ๊ณ ์ ํ๋ ๋ฌธ์ ์๋ค.
Union-Find๋ฅผ ํ์ฉํ ๋ถ๋ฆฌ ์งํฉ์ผ๋ก ์ฌ์ดํด์ ์ฐพ์๋ผ ์ ์์๊ฑฐ๋ผ ์๊ฐํ์์ผ๋ Union-Find๋ ๋จ์ ์ฌ์ดํด ์กด์ฌ ์ฌ๋ถ๋ง ํ๋จํ ์ ์๊ณ , ์ด๋ค ๋ ธ๋๋ค์ด ์ฌ์ดํด์ ๊ตฌ์ฑํ๋์ง๋ ์์๋ผ ์๊ฐ ์๋ค. ๋ฐ๋ผ์ Union-Find๋ ์ ํฉํ ํ์ด ๋ฐฉ๋ฒ์ด ์๋์๋ค.
DFS๋ฅผ ์ด์ฉํ์ฌ ์ฌ์ดํด์ ์ฐพ์๋ค์ BFS๋ก ์ฌ์ดํด - ๋ ธ๋๊ฐ์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ ๋ฐฉ์์ด ์ ์ ํ์ด์ธ ์ค ์์์ผ๋ ๊ฐ ๋ ธ๋์ ์ฐจ์(degree)๋ฅผ ์ด์ฉํด leaf_node๋ฅผ ์ ๊ฑฐํ์ฌ ์ฌ์ดํด์ ์ฐพ์๋ด๋ ๊ฒ์ด ์ ์์ธ ๋ฌธ์ ์๋ค. ์๊ฐ๋ ํจ์ฌ ๋น ๋ฅด๊ณ ๊ตฌํ๋ DFS๋ณด๋ค ์ฌ์ด ๊ฒ ๊ฐ๋ค.
'Coding-Test > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Python] 2342 Dance Dance Revolution (0) | 2026.03.20 |
|---|---|
| [Python] 11049 ํ๋ ฌ ๊ณฑ์ ์์ (0) | 2026.03.20 |
| [Python] 2613 ์ซ์๊ตฌ์ฌ (0) | 2026.03.18 |
| [Python] 16946 ๋ฒฝ ๋ถ์๊ณ ์ด๋ํ๊ธฐ 4 (0) | 2026.03.17 |
| [Python] 1765 ๋ญ์ธ์ ํ ์ ํ๊ธฐ (0) | 2026.03.03 |