File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments