Submission #7863838


Source Code Expand

Copy
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    s = S()
    k = I()
    n = len(s)
    for i in range(n):
        if s[i] != s[0]:
            break
    else:
        print((k*n)>>1)
        return
    s += s
    t = s[i-1]
    for j in range(n)[::-1]:
        if s[j] != t:
            break
    s = s[i:j+1]
    ans = (i>>1)+((n-1-j)>>1)+((n-1-j+i)>>1)*(k-1)
    i = 0
    n = len(s)
    while i < n:
        t = s[i]
        l = 0
        while i < n and t == s[i]:
            i += 1
            l += 1
        ans += (l >> 1)*k
    print(ans)
    return

#B
def B():
    n = I()
    s = [list(map(int, input())) for i in range(n)]
    v = [[] for i in range(n)]
    for i in range(n):
        for j  in range(i+1,n):
            if s[i][j]:
                v[i].append(j)
                v[j].append(i)
    bfs = [-1]*n
    q = deque([0])
    q[0] = 0
    f = 1
    while f and q:
        x = q.popleft()
        nb = 1-bfs[x]
        for y in v[x]:
            if bfs[y] >= 0:
                if bfs[y] != nb:
                    f = 0
                    break
            else:
                bfs[y] = nb
                q.append(y)
    if not f:
        print(-1)
        return
    ans = -1
    for i in range(n):
        bfs = [0]*n
        bfs[i] = 1
        q.append(i)
        while q:
            x = q.popleft()
            nb = bfs[x]+1
            for y in v[x]:
                if not bfs[y]:
                    bfs[y] = nb
                    q.append(y)
        m = max(bfs)
        if m > ans:
            ans = m
    print(ans)
    return

#C
def C():

    return

#D
def D():

    return

#E
def E():

    return

#F
def F():

    return

#Solve
if __name__ == "__main__":
    B()

Submission Info

Submission Time
Task B - Graph Partition
User dn6049949
Language PyPy3 (2.4.0)
Score 500
Code Size 2469 Byte
Status AC
Exec Time 270 ms
Memory 46064 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 55
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, 49.txt, 50.txt, 51.txt, 52.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt AC 270 ms 46064 KB
02.txt AC 207 ms 42656 KB
03.txt AC 208 ms 42204 KB
04.txt AC 224 ms 44252 KB
05.txt AC 227 ms 43376 KB
06.txt AC 201 ms 42076 KB
07.txt AC 223 ms 43484 KB
08.txt AC 230 ms 44380 KB
09.txt AC 200 ms 41200 KB
10.txt AC 212 ms 42204 KB
11.txt AC 202 ms 42204 KB
12.txt AC 215 ms 42844 KB
13.txt AC 220 ms 43120 KB
14.txt AC 205 ms 42076 KB
15.txt AC 237 ms 44124 KB
16.txt AC 211 ms 42076 KB
17.txt AC 232 ms 43376 KB
18.txt AC 229 ms 44380 KB
19.txt AC 227 ms 43996 KB
20.txt AC 234 ms 44380 KB
21.txt AC 216 ms 42736 KB
22.txt AC 222 ms 43996 KB
23.txt AC 221 ms 43356 KB
24.txt AC 233 ms 44508 KB
25.txt AC 228 ms 43376 KB
26.txt AC 227 ms 44252 KB
27.txt AC 228 ms 44252 KB
28.txt AC 241 ms 44508 KB
29.txt AC 225 ms 43248 KB
30.txt AC 232 ms 44124 KB
31.txt AC 221 ms 43228 KB
32.txt AC 235 ms 44252 KB
33.txt AC 215 ms 41968 KB
34.txt AC 227 ms 43100 KB
35.txt AC 202 ms 41948 KB
36.txt AC 198 ms 41948 KB
37.txt AC 235 ms 43376 KB
38.txt AC 244 ms 44508 KB
39.txt AC 219 ms 42844 KB
40.txt AC 214 ms 42844 KB
41.txt AC 204 ms 40944 KB
42.txt AC 217 ms 43228 KB
43.txt AC 204 ms 41836 KB
44.txt AC 225 ms 43740 KB
45.txt AC 199 ms 40816 KB
46.txt AC 226 ms 43484 KB
47.txt AC 229 ms 43996 KB
48.txt AC 231 ms 43868 KB
49.txt AC 183 ms 39920 KB
50.txt AC 183 ms 39920 KB
51.txt AC 186 ms 39920 KB
52.txt AC 176 ms 39920 KB
s1.txt AC 185 ms 39920 KB
s2.txt AC 193 ms 39920 KB
s3.txt AC 180 ms 39920 KB