Skip to content

Commit bbd2ae8

Browse files
committed
95차 2번 문제 풀이
1 parent 1d272ba commit bbd2ae8

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

live9/test95/문제2/박희경.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from collections import *
2+
3+
4+
5+
def solution(n, wires):
6+
7+
def create_graph(wires):
8+
graph = [[] for _ in range(n + 1)]
9+
10+
for v1, v2 in wires:
11+
graph[v1].append(v2)
12+
graph[v2].append(v1)
13+
return graph
14+
15+
def bfs(graph, x, visited):
16+
cnt = 1
17+
q = deque([x])
18+
visited[x] = 1
19+
while q:
20+
x = q.popleft()
21+
for nx in graph[x]:
22+
if not visited[nx]:
23+
visited[nx] = 1
24+
cnt += 1
25+
q.append(nx)
26+
return cnt
27+
28+
answer = float('inf')
29+
30+
# 모든 간선 하나씩 끊어보기
31+
for i in range(len(wires)):
32+
tmp = wires[:]
33+
del tmp[i]
34+
tmp_graph = create_graph(tmp)
35+
36+
visited = [0] * (n + 1)
37+
count = []
38+
39+
for num in range(1, n + 1):
40+
if not visited[num]:
41+
count.append(bfs(tmp_graph, num, visited))
42+
43+
diff = abs(count[0] - count[1])
44+
answer = min(answer, diff)
45+
46+
return answer

0 commit comments

Comments
 (0)