Skip to content

Commit

Permalink
46!
Browse files Browse the repository at this point in the history
  • Loading branch information
park-jiyeong committed Aug 13, 2024
1 parent 6599d16 commit ce1b8c0
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions JYPARK/41to50/46.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from collections import deque

def bfs(flag, tree):
queue = deque()
queue.append(flag)
visited = []
answer = 0
while queue:
x = queue.popleft()
visited.append(x)
for i in range(len(tree[x])):
if tree[x][i] != 0 and i not in visited:
queue.append(i)
answer += 1
return answer


def solution(n, wires):
answer = 101
delete = 0
for i in range(len(wires)):
tree = [[0]*(n+1) for _ in range(n+1)]
for j in range(len(wires)):
if j != delete:
tree[wires[j][0]][wires[j][1]] = 1
tree[wires[j][1]][wires[j][0]] = 1
a = bfs(wires[delete][0], tree)
b = bfs(wires[delete][1], tree)
delete += 1
answer = min(answer, (abs(a - b)))

return answer

wires, n = [[1,3],[2,3],[3,4],[4,5],[4,6],[4,7],[7,8],[7,9]], 9
wires, n = [[1,2],[2,3],[3,4]], 4
wires, n = [[1,2],[2,7],[3,7],[3,4],[4,5],[6,7]], 7
print(solution(n, wires))

0 comments on commit ce1b8c0

Please sign in to comment.