Submission #66751926
Source Code Expand
Copy
from collections import dequen, m = map(int, input().split())p = [[*map(int, input().split())] for _ in range(m)]g = [[] for _ in range(n)]rg = [[] for _ in range(n)]for i in range(m):a, b, w = p[i]a -= 1b -= 1g[a].append([b, w])rg[b].append(a)visited = [False] * ndq = deque([0])visited[0] = Truewhile dq:u = dq.popleft()for v, _ in g[u]:
from collections import deque
n, m = map(int, input().split())
p = [[*map(int, input().split())] for _ in range(m)]
g = [[] for _ in range(n)]
rg = [[] for _ in range(n)]
for i in range(m):
a, b, w = p[i]
a -= 1
b -= 1
g[a].append([b, w])
rg[b].append(a)
visited = [False] * n
dq = deque([0])
visited[0] = True
while dq:
u = dq.popleft()
for v, _ in g[u]:
if not visited[v]:
visited[v] = True
dq.append(v)
if not visited[n - 1]:
print(-1)
exit()
# 逆向きを考えて1->Nへのパスを保証する
reverse_visited = [False] * n
dq = deque([n - 1])
reverse_visited[n - 1] = True
while dq:
u = dq.popleft()
for v in rg[u]:
if not reverse_visited[v]:
reverse_visited[v] = True
dq.append(v)
seen = [False] * n
d = [0] * n
dq = deque([0])
seen[0] = True
while dq:
u = dq.popleft()
for v, w in g[u]:
if not (visited[v] and reverse_visited[v]):
continue
if not seen[v]:
seen[v] = True
d[v] = d[u] ^ w
dq.append(v)
max_xor = max((w for _, _, w in p))
max_length = max(max_xor.bit_length(), d[n - 1].bit_length())
basis = [0] * (max_length + 1)
for u, v, w in p:
u -= 1
v -= 1
if visited[u] and visited[v] and reverse_visited[u] and reverse_visited[v]:
c = d[u] ^ w ^ d[v]
if c:
for b in range(max_length, -1, -1):
if not (c >> b) & 1:
continue
if basis[b] == 0:
basis[b] = c
break
c ^= basis[b]
ans = d[-1]
for b in range(max_length, -1, -1):
if basis[b] and (ans ^ basis[b]) < ans:
ans ^= basis[b]
print(ans)
Submission Info
| Submission Time | |
|---|---|
| Task | D - XOR Shortest Walk |
| User | fmuuly |
| Language | Python (PyPy 3.10-v7.3.12) |
| Score | 0 |
| Code Size | 1838 Byte |
| Status | WA |
| Exec Time | 82 ms |
| Memory | 81864 KiB |
Judge Result
| Set Name | Sample | All | ||||||
|---|---|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 0 / 400 | ||||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | sample_01.txt, sample_02.txt, sample_03.txt |
| All | hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, sample_01.txt, sample_02.txt, sample_03.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| hand_01.txt | AC | 68 ms | 76652 KiB |
| hand_02.txt | AC | 68 ms | 77224 KiB |
| hand_03.txt | AC | 67 ms | 76776 KiB |
| hand_04.txt | AC | 67 ms | 77120 KiB |
| hand_05.txt | AC | 68 ms | 76628 KiB |
| hand_06.txt | AC | 68 ms | 76732 KiB |
| hand_07.txt | WA | 67 ms | 76964 KiB |
| hand_08.txt | WA | 67 ms | 76840 KiB |
| random_01.txt | AC | 68 ms | 76876 KiB |
| random_02.txt | AC | 76 ms | 81216 KiB |
| random_03.txt | AC | 68 ms | 77224 KiB |
| random_04.txt | AC | 74 ms | 81192 KiB |
| random_05.txt | AC | 68 ms | 76844 KiB |
| random_06.txt | AC | 70 ms | 76792 KiB |
| random_07.txt | AC | 67 ms | 76860 KiB |
| random_08.txt | AC | 75 ms | 81548 KiB |
| random_09.txt | AC | 68 ms | 76924 KiB |
| random_10.txt | AC | 80 ms | 81428 KiB |
| random_11.txt | AC | 68 ms | 77108 KiB |
| random_12.txt | AC | 76 ms | 81340 KiB |
| random_13.txt | AC | 76 ms | 81864 KiB |
| random_14.txt | AC | 75 ms | 81748 KiB |
| random_15.txt | AC | 73 ms | 81772 KiB |
| random_16.txt | AC | 68 ms | 76856 KiB |
| random_17.txt | AC | 81 ms | 81572 KiB |
| random_18.txt | AC | 82 ms | 81764 KiB |
| random_19.txt | AC | 78 ms | 81816 KiB |
| random_20.txt | AC | 79 ms | 81860 KiB |
| random_21.txt | AC | 81 ms | 81644 KiB |
| random_22.txt | AC | 81 ms | 81552 KiB |
| sample_01.txt | AC | 67 ms | 76912 KiB |
| sample_02.txt | AC | 68 ms | 76916 KiB |
| sample_03.txt | AC | 67 ms | 76928 KiB |