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 sys
2+ from collections import *
3+
4+ input = sys .stdin .readline
5+
6+ n , m = map (int ,input ().split ())
7+
8+ picture = []
9+ for _ in range (n ):
10+ picture .append (list (map (int ,input ().split ())))
11+
12+
13+ dx = [- 1 , 1 , 0 , 0 ]
14+ dy = [0 , 0 , - 1 , 1 ]
15+ visited = [[0 ] * m for _ in range (n )]
16+
17+
18+ def bfs (x , y ):
19+ q = deque ([(x , y )])
20+ size = 1
21+ visited [x ][y ] = 1
22+ while q :
23+ x , y = q .popleft ()
24+ for i in range (4 ):
25+ nx , ny = x + dx [i ], y + dy [i ]
26+ if 0 <= nx < n and 0 <= ny < m and not visited [nx ][ny ]:
27+ if picture [nx ][ny ] == 1 :
28+ size += 1
29+ visited [nx ][ny ] = 1
30+ q .append ((nx , ny ))
31+ return size
32+
33+
34+ cnt = 0
35+ max_extent = 0
36+ for i in range (n ):
37+ for j in range (m ):
38+ if picture [i ][j ] == 1 and not visited [i ][j ]:
39+ max_extent = max (bfs (i , j ), max_extent )
40+ cnt += 1
41+ print (cnt )
42+ print (max_extent )
43+
44+ """
45+ 6 5
46+ 1 1 0 1 1
47+ 0 1 1 0 0
48+ 0 0 0 0 0
49+ 1 0 1 1 1
50+ 0 0 1 1 1
51+ 0 0 1 1 1
52+ """
Original file line number Diff line number Diff line change 1+ import sys
2+ from collections import *
3+
4+ input = sys .stdin .readline
5+
6+ n , m = map (int , input ().split ())
7+ r = [[] for _ in range (n + 1 )]
8+ for _ in range (m ):
9+ a , b = map (int , input ().split ())
10+ r [a ].append (b )
11+ r [b ].append (a )
12+
13+
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 ))
26+
27+
28+ kb = [float ("inf" )] + []
29+ for i in range (1 , n + 1 ):
30+ i_kb = 0
31+ for j in range (1 , n + 1 ):
32+ if i != j :
33+ i_kb += bfs (i , j , 0 )
34+ kb .append (i_kb )
35+ print (kb .index (min (kb )))
36+
37+ """
38+ 5 5
39+ 1 3
40+ 1 4
41+ 4 5
42+ 4 3
43+ 3 2
44+ """
You can’t perform that action at this time.
0 commit comments