Skip to content

Commit f63b6de

Browse files
committed
95차 2번 문제풀이
1 parent 1c81cf2 commit f63b6de

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

live9/test95/문제2/백한결.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def solution(n, wires):
2+
3+
min_difference = float('inf')
4+
5+
for i in range(len(wires)):
6+
graph = {j: [] for j in range(1, n+1)}
7+
8+
for j, (v1, v2) in enumerate(wires):
9+
if i != j:
10+
graph[v1].append(v2)
11+
graph[v2].append(v1)
12+
13+
visited = [False] * (n+1)
14+
count = dfs(1, graph, visited)
15+
16+
min_difference = min(min_difference, abs(count-(n-count)))
17+
18+
return min_difference
19+
20+
21+
def dfs(start, graph, visited):
22+
visited[start] = True
23+
count = 1
24+
25+
for neighbor in graph[start]:
26+
if not visited[neighbor]:
27+
count += dfs(neighbor, graph, visited)
28+
29+
return count
30+
31+
print(solution(9, [[1,3],[2,3],[3,4],[4,5],[4,6],[4,7],[7,8],[7,9]]))

0 commit comments

Comments
 (0)