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- import math
21import sys
2+ from collections import *
33
44input = sys .stdin .readline
55
1111 r [b ].append (a )
1212
1313
14- def dfs (x , target , depth ):
15- if x == target :
16- return depth
17- if len (r [x ]) != 0 :
18- for i in r [x ]:
19- if not visited [x ]:
20- visited [x ] = 1
21- depth += 1
22- dfs (i , target , depth )
14+ def bfs (x , target , depth ):
15+ visited = [0 ] * (n + 1 )
16+ q = deque ([(x , depth )])
17+ visited [x ] = 1
18+ while q :
19+ x , depth = q .popleft ()
20+ if x == target :
21+ return depth
22+ for nx in r [x ]:
23+ if not visited [nx ]:
24+ visited [nx ] = 1
25+ q .append ((nx , depth + 1 ))
2326
2427
25- min_kb = float ("- inf" )
28+ kb = [ float ("inf" )] + []
2629for i in range (1 , n + 1 ):
2730 i_kb = 0
2831 for j in range (1 , n + 1 ):
2932 if i != j :
30- visited = [0 ] * (n + 1 )
31- i_kb += dfs (i , j )
32- min_kb = min (min_kb , i_kb )
33-
33+ i_kb += bfs (i , j , 0 )
34+ kb .append (i_kb )
35+ print (kb .index (min (kb )))
3436
3537"""
36385 5
You can’t perform that action at this time.
0 commit comments