Submission #63034069
Source Code Expand
Copy
from collections import dequeN = int(input())graph = {}graphInv = {}for i in range(N):C = input()graph[i] = []for j in range(N):if C[j] != "-":graph[i].append([j, C[j]])if j not in graphInv:graphInv[j] = []graphInv[j].append([i, C[j]])def getMinPalindromic(a,b):queue = deque([(a,b,set([]),0)])while queue:a,b,alreadyVisited,dist = queue.popleft()if a == b:return dist
from collections import deque
N = int(input())
graph = {}
graphInv = {}
for i in range(N):
C = input()
graph[i] = []
for j in range(N):
if C[j] != "-":
graph[i].append([j, C[j]])
if j not in graphInv:
graphInv[j] = []
graphInv[j].append([i, C[j]])
def getMinPalindromic(a,b):
queue = deque([(a,b,set([]),0)])
while queue:
a,b,alreadyVisited,dist = queue.popleft()
if a == b:
return dist
characters = set([])
characterDict = {}
for i in graph[a]:
characters.add(i[1])
if i[1] not in characterDict:
characterDict[i[1]] = []
characterDict[i[1]].append(i[0])
characters2 = set([])
characterDict2 = {}
for i in graphInv[b]:
characters2.add(i[1])
if i[1] not in characterDict2:
characterDict2[i[1]] = []
characterDict2[i[1]].append(i[0])
characters = characters & characters2
if len(characters) == 0:
return -1
mini = 99999999999999
for i in characters:
for j in characterDict[i]:
if j == b:
return 1+dist
for k in characterDict2[i]:
if k == a:
return 1+dist
if not (j,k) in alreadyVisited:
copy = alreadyVisited.copy()
copy.add((j,k))
queue.append((j,k,copy,2+dist))
return -1
for i in range(N):
ans = []
for j in range(N):
ans.append(getMinPalindromic(i,j))
print(" ".join(map(str,ans)))
Submission Info
| Submission Time | |
|---|---|
| Task | E - Palindromic Shortest Path |
| User | kangping |
| Language | Python (CPython 3.11.4) |
| Score | 0 |
| Code Size | 1770 Byte |
| Status | RE |
| Exec Time | 2208 ms |
| Memory | 14264 KiB |
Judge Result
| Set Name | Sample | All | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 0 / 450 | ||||||||||
| Status |
|
|
| 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 | 12 ms | 9464 KiB |
| sample01.txt | AC | 12 ms | 9476 KiB |
| testcase00.txt | AC | 12 ms | 9432 KiB |
| testcase01.txt | AC | 12 ms | 9472 KiB |
| testcase02.txt | AC | 34 ms | 9708 KiB |
| testcase03.txt | AC | 655 ms | 11172 KiB |
| testcase04.txt | RE | 12 ms | 9508 KiB |
| testcase05.txt | RE | 13 ms | 9556 KiB |
| testcase06.txt | RE | 13 ms | 9580 KiB |
| testcase07.txt | RE | 13 ms | 9476 KiB |
| testcase08.txt | WA | 32 ms | 9532 KiB |
| testcase09.txt | WA | 45 ms | 9516 KiB |
| testcase10.txt | WA | 23 ms | 9608 KiB |
| testcase11.txt | WA | 46 ms | 9512 KiB |
| testcase12.txt | WA | 36 ms | 9528 KiB |
| testcase13.txt | WA | 43 ms | 9596 KiB |
| testcase14.txt | WA | 23 ms | 9564 KiB |
| testcase15.txt | WA | 47 ms | 9508 KiB |
| testcase16.txt | RE | 13 ms | 9436 KiB |
| testcase17.txt | RE | 13 ms | 9496 KiB |
| testcase18.txt | RE | 13 ms | 9496 KiB |
| testcase19.txt | RE | 13 ms | 9584 KiB |
| testcase20.txt | RE | 13 ms | 9520 KiB |
| testcase21.txt | TLE | 2208 ms | 12832 KiB |
| testcase22.txt | WA | 964 ms | 11384 KiB |
| testcase23.txt | TLE | 2208 ms | 14240 KiB |
| testcase24.txt | WA | 263 ms | 10820 KiB |
| testcase25.txt | TLE | 2208 ms | 14264 KiB |
| testcase26.txt | RE | 12 ms | 9524 KiB |
| testcase27.txt | WA | 1221 ms | 10704 KiB |
| testcase28.txt | WA | 567 ms | 10624 KiB |
| testcase29.txt | TLE | 2208 ms | 14112 KiB |
| testcase30.txt | WA | 336 ms | 10896 KiB |
| testcase31.txt | TLE | 2208 ms | 13380 KiB |
| testcase32.txt | RE | 13 ms | 9516 KiB |
| testcase33.txt | WA | 885 ms | 10652 KiB |
| testcase34.txt | WA | 173 ms | 10208 KiB |
| testcase35.txt | WA | 1681 ms | 11440 KiB |
| testcase36.txt | WA | 409 ms | 10504 KiB |
| testcase37.txt | TLE | 2208 ms | 12616 KiB |
| testcase38.txt | RE | 13 ms | 9532 KiB |
| testcase39.txt | WA | 771 ms | 10372 KiB |
| testcase40.txt | WA | 987 ms | 10948 KiB |
| testcase41.txt | WA | 1258 ms | 11168 KiB |
| testcase42.txt | WA | 283 ms | 10288 KiB |
| testcase43.txt | WA | 1720 ms | 12048 KiB |
| testcase44.txt | RE | 13 ms | 9600 KiB |
| testcase45.txt | WA | 74 ms | 9688 KiB |
| testcase46.txt | WA | 51 ms | 9560 KiB |
| testcase47.txt | WA | 327 ms | 9844 KiB |
| testcase48.txt | WA | 334 ms | 9808 KiB |
| testcase49.txt | WA | 489 ms | 9904 KiB |
| testcase50.txt | WA | 230 ms | 9760 KiB |
| testcase51.txt | WA | 508 ms | 10144 KiB |
| testcase52.txt | WA | 93 ms | 9728 KiB |
| testcase53.txt | WA | 535 ms | 10312 KiB |
| testcase54.txt | WA | 355 ms | 10136 KiB |
| testcase55.txt | WA | 541 ms | 10360 KiB |