-
Notifications
You must be signed in to change notification settings - Fork 0
/
전력망을 둘로 나누기.py
48 lines (35 loc) · 994 Bytes
/
전력망을 둘로 나누기.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from copy import deepcopy
def dfs(now, graph, visited):
visited[now] = True
st = [now]
cnt = 1
while st:
v = st.pop()
for w in graph[v]:
if not visited[w]:
visited[w] = True
st.append(w)
cnt += 1
return cnt, visited
def full_search(n, graph, visited):
counts = []
for i in range(1, n + 1):
if visited[i]:
continue
cnt, visited = dfs(i, graph, visited)
counts.append(cnt)
return counts
def solution(n, wires):
ret = 1e9
graph = [[] for _ in range(n + 1)]
for u, v in wires:
graph[u].append(v)
graph[v].append(u)
for u, v in wires:
g = deepcopy(graph)
g[u].remove(v)
g[v].remove(u)
counts = full_search(n, g, [False] * (n + 1))
if len(counts) == 2:
ret = min(ret, abs(counts[0] - counts[1]))
return ret