Submission #63061322


Source Code Expand

Copy
from collections import deque, defaultdict
N = int(input())
C = [input() for _ in range(N)]
tree = [[] for _ in range(N)]
rtree = [[] for _ in range(N)]
group_t = [defaultdict(list) for _ in range(N)]
group_r = [defaultdict(list) for _ in range(N)]
for i in range(N):
for j in range(N):
nm = C[i][j]
if nm != '-':
tree[i].append((j,nm))
rtree[j].append((i,nm))
group_t[i][nm].append(j)
group_r[j][nm].append(i)
INF = 4*N
dp = [[INF]*N for _ in range(N)]
Q = deque()
for i in range(N):
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
from collections import deque, defaultdict
N = int(input())
C = [input() for _ in range(N)]
tree = [[] for _ in range(N)]
rtree = [[] for _ in range(N)]
group_t = [defaultdict(list) for _ in range(N)]
group_r = [defaultdict(list) for _ in range(N)]

for i in range(N):
    for j in range(N):
        nm = C[i][j]
        if nm != '-':
            tree[i].append((j,nm))
            rtree[j].append((i,nm))
            group_t[i][nm].append(j)
            group_r[j][nm].append(i)
    
INF = 4*N
dp = [[INF]*N for _ in range(N)]
Q = deque()
for i in range(N):
    dp[i][i] = 0
    Q.append((i, i))
for i in range(N):
    for j,nm in tree[i]:
        if dp[i][j] > 1:
            dp[i][j] = 1
            Q.append((i,j))
    
while Q:
    i,j = Q.popleft()
    d = dp[i][j]
    for c in group_r[i]:
        if c in group_t[j]:
            for p in group_r[i][c]:
                for q in group_t[j][c]:
                    nd = d+2
                    if nd < dp[p][q]:
                        dp[p][q] = nd
                        Q.append((p,q))
    

for s in range(N):
    ans = []
    for t in range(N):
        ans.append(dp[s][t] if dp[s][t] != INF else -1)
    print(*ans)

Submission Info

Submission Time
Task E - Palindromic Shortest Path
User sA_3
Language Python (PyPy 3.10-v7.3.12)
Score 450
Code Size 1228 Byte
Status AC
Exec Time 178 ms
Memory 86652 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 2
AC × 58
Set Name Test Cases
Sample sample00.txt, sample01.txt
All sample00.txt, sample01.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt, testcase28.txt, testcase29.txt, testcase30.txt, testcase31.txt, testcase32.txt, testcase33.txt, testcase34.txt, testcase35.txt, testcase36.txt, testcase37.txt, testcase38.txt, testcase39.txt, testcase40.txt, testcase41.txt, testcase42.txt, testcase43.txt, testcase44.txt, testcase45.txt, testcase46.txt, testcase47.txt, testcase48.txt, testcase49.txt, testcase50.txt, testcase51.txt, testcase52.txt, testcase53.txt, testcase54.txt, testcase55.txt
Case Name Status Exec Time Memory
sample00.txt AC 68 ms 76792 KB
sample01.txt AC 67 ms 76976 KB
testcase00.txt AC 68 ms 77092 KB
testcase01.txt AC 67 ms 76836 KB
testcase02.txt AC 100 ms 84060 KB
testcase03.txt AC 178 ms 86652 KB
testcase04.txt AC 77 ms 82044 KB
testcase05.txt AC 79 ms 82092 KB
testcase06.txt AC 81 ms 83340 KB
testcase07.txt AC 81 ms 83488 KB
testcase08.txt AC 83 ms 83848 KB
testcase09.txt AC 88 ms 83860 KB
testcase10.txt AC 79 ms 82740 KB
testcase11.txt AC 88 ms 84188 KB
testcase12.txt AC 87 ms 83984 KB
testcase13.txt AC 86 ms 83908 KB
testcase14.txt AC 82 ms 84024 KB
testcase15.txt AC 87 ms 84168 KB
testcase16.txt AC 77 ms 81832 KB
testcase17.txt AC 81 ms 83668 KB
testcase18.txt AC 77 ms 81760 KB
testcase19.txt AC 82 ms 83484 KB
testcase20.txt AC 88 ms 83852 KB
testcase21.txt AC 97 ms 84516 KB
testcase22.txt AC 93 ms 84352 KB
testcase23.txt AC 107 ms 84604 KB
testcase24.txt AC 96 ms 84172 KB
testcase25.txt AC 127 ms 84992 KB
testcase26.txt AC 98 ms 83944 KB
testcase27.txt AC 100 ms 84316 KB
testcase28.txt AC 99 ms 84436 KB
testcase29.txt AC 127 ms 85288 KB
testcase30.txt AC 97 ms 84048 KB
testcase31.txt AC 116 ms 84688 KB
testcase32.txt AC 104 ms 84788 KB
testcase33.txt AC 113 ms 84860 KB
testcase34.txt AC 101 ms 84276 KB
testcase35.txt AC 111 ms 84540 KB
testcase36.txt AC 97 ms 84364 KB
testcase37.txt AC 115 ms 84924 KB
testcase38.txt AC 103 ms 84280 KB
testcase39.txt AC 105 ms 84188 KB
testcase40.txt AC 111 ms 85068 KB
testcase41.txt AC 111 ms 85416 KB
testcase42.txt AC 103 ms 84464 KB
testcase43.txt AC 133 ms 85508 KB
testcase44.txt AC 89 ms 84068 KB
testcase45.txt AC 94 ms 84196 KB
testcase46.txt AC 97 ms 84324 KB
testcase47.txt AC 101 ms 84736 KB
testcase48.txt AC 109 ms 84924 KB
testcase49.txt AC 108 ms 84684 KB
testcase50.txt AC 106 ms 85076 KB
testcase51.txt AC 120 ms 85232 KB
testcase52.txt AC 94 ms 83952 KB
testcase53.txt AC 123 ms 85856 KB
testcase54.txt AC 116 ms 85668 KB
testcase55.txt AC 134 ms 85792 KB


2025-03-29 (Sat)
07:31:02 +00:00