Submission #66787076


Source Code Expand

Copy
from collections import defaultdict, deque
def insert_basis(basis, x):
for b in basis:
x = min(x, x ^ b)
if x:
basis.append(x)
basis.sort(reverse=True) # optional
return basis
n, m = map(int, input().split())
d = defaultdict(list)
for _ in range(m):
a, b, w = map(int, input().split())
a -= 1
b -= 1
d[a].append((b, w))
d[b].append((a, w))
visited = [None] * n
basis = []
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
from collections import defaultdict, deque

def insert_basis(basis, x):
    for b in basis:
        x = min(x, x ^ b)
    if x:
        basis.append(x)
        basis.sort(reverse=True)  # optional
    return basis

n, m = map(int, input().split())
d = defaultdict(list)
for _ in range(m):
    a, b, w = map(int, input().split())
    a -= 1
    b -= 1
    d[a].append((b, w))
    d[b].append((a, w))

visited = [None] * n
basis = []

que = deque([(0, 0)])
visited[0] = 0

while que:
    v, xor_val = que.popleft()
    for u, w in d[v]:
        new_xor = xor_val ^ w
        if visited[u] is None:
            visited[u] = new_xor
            que.append((u, new_xor))
        else:
            cycle_xor = new_xor ^ visited[u]
            basis = insert_basis(basis, cycle_xor)

# 到達不可
if visited[n - 1] is None:
    print(-1)
else:
    res = visited[n - 1]
    for b in basis:
        res = min(res, res ^ b)
    print(res)

Submission Info

Submission Time
Task D - XOR Shortest Walk
User jacobiIdentity
Language Python (PyPy 3.10-v7.3.12)
Score 0
Code Size 975 Byte
Status WA
Exec Time 87 ms
Memory 83692 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
AC × 3
AC × 28
WA × 5
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 77196 KiB
hand_02.txt AC 68 ms 77236 KiB
hand_03.txt WA 68 ms 77068 KiB
hand_04.txt AC 67 ms 76956 KiB
hand_05.txt WA 68 ms 76896 KiB
hand_06.txt WA 68 ms 76876 KiB
hand_07.txt WA 67 ms 77232 KiB
hand_08.txt WA 67 ms 77168 KiB
random_01.txt AC 68 ms 76884 KiB
random_02.txt AC 78 ms 81420 KiB
random_03.txt AC 67 ms 76888 KiB
random_04.txt AC 72 ms 76832 KiB
random_05.txt AC 68 ms 76688 KiB
random_06.txt AC 70 ms 76872 KiB
random_07.txt AC 67 ms 76924 KiB
random_08.txt AC 76 ms 81796 KiB
random_09.txt AC 68 ms 76824 KiB
random_10.txt AC 79 ms 81840 KiB
random_11.txt AC 68 ms 76824 KiB
random_12.txt AC 76 ms 81248 KiB
random_13.txt AC 75 ms 80968 KiB
random_14.txt AC 76 ms 81416 KiB
random_15.txt AC 73 ms 81168 KiB
random_16.txt AC 69 ms 76892 KiB
random_17.txt AC 85 ms 83552 KiB
random_18.txt AC 86 ms 83692 KiB
random_19.txt AC 87 ms 83376 KiB
random_20.txt AC 87 ms 83444 KiB
random_21.txt AC 81 ms 81760 KiB
random_22.txt AC 81 ms 82200 KiB
sample_01.txt AC 67 ms 77128 KiB
sample_02.txt AC 68 ms 76832 KiB
sample_03.txt AC 68 ms 76884 KiB


2025-06-16 (Mon)
14:45:53 +09:00