Submission #66779903


Source Code Expand

Copy
from collections import deque
def main():
N, M = map(int, input().split())
C = [[] for _ in range(N)]
for _ in range(M):
a, b, w = map(int, input().split())
a, b = a - 1, b - 1
C[a].append((b, w))
seen = [False] * N
dfs = deque()
# (node, xor)
dfs.append((0, 0))
seen[0] = True
cycle1 = [0]
path = []
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
from collections import deque

def main():
    N, M = map(int, input().split())
    
    C = [[] for _ in range(N)]

    for _ in range(M):
        a, b, w = map(int, input().split())
        a, b = a - 1, b - 1
        C[a].append((b, w))

    seen = [False] * N

    dfs = deque()
    # (node, xor)
    dfs.append((0, 0))
    seen[0] = True
    
    cycle1 = [0]
    path = []
    
    while dfs:
        node, xor = dfs.pop()
        for to, cost in C[node]:
            if to == 0:
                cycle1.append((xor ^ cost))
            
            elif to == N - 1:
                path.append((xor ^ cost))

            else:
                if not seen[to]:
                    seen[to] = True
                    dfs.append((to, xor ^ cost))

    dfs = deque()
    dfs.append((N-1, 0))
    seen = [False] * N
    seen[N-1] = True
    cycleN = [0]
    while dfs:
        node, xor = dfs.pop()
        for to, cost in C[node]:
            if to == N - 1:
                cycleN.append((xor ^ cost))
            else:
                if not seen[to]:
                    seen[to] = True
                    dfs.append((to, xor ^ cost))

    # print(cycle1)
    # print(path)
    # print(cycleN)

    if len(path) == 0:
        print(-1)
    else:
        ans = 10 ** 18
        for c1 in cycle1:
            for p in path:
                for cN in cycleN:
                    ans = min(ans, c1 ^ p ^ cN)
        print(ans)

    return

main()

Submission Info

Submission Time
Task D - XOR Shortest Walk
User yyoshi
Language Python (PyPy 3.10-v7.3.12)
Score 0
Code Size 1517 Byte
Status WA
Exec Time 75 ms
Memory 81220 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
AC × 3
AC × 29
WA × 4
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 65 ms 76872 KiB
hand_02.txt AC 65 ms 76956 KiB
hand_03.txt AC 65 ms 76832 KiB
hand_04.txt AC 65 ms 77024 KiB
hand_05.txt AC 67 ms 77108 KiB
hand_06.txt AC 66 ms 76976 KiB
hand_07.txt AC 66 ms 76776 KiB
hand_08.txt AC 66 ms 76932 KiB
random_01.txt AC 66 ms 76820 KiB
random_02.txt AC 70 ms 76836 KiB
random_03.txt AC 66 ms 76820 KiB
random_04.txt AC 70 ms 76812 KiB
random_05.txt AC 66 ms 76852 KiB
random_06.txt AC 68 ms 76972 KiB
random_07.txt AC 67 ms 76808 KiB
random_08.txt AC 71 ms 76804 KiB
random_09.txt AC 66 ms 76808 KiB
random_10.txt AC 73 ms 80796 KiB
random_11.txt AC 65 ms 76796 KiB
random_12.txt AC 71 ms 76872 KiB
random_13.txt WA 68 ms 76636 KiB
random_14.txt WA 69 ms 77164 KiB
random_15.txt AC 69 ms 81220 KiB
random_16.txt AC 66 ms 76856 KiB
random_17.txt AC 74 ms 81024 KiB
random_18.txt AC 75 ms 80872 KiB
random_19.txt AC 75 ms 81096 KiB
random_20.txt AC 75 ms 80840 KiB
random_21.txt WA 75 ms 80936 KiB
random_22.txt WA 75 ms 81172 KiB
sample_01.txt AC 65 ms 76840 KiB
sample_02.txt AC 65 ms 77024 KiB
sample_03.txt AC 64 ms 76728 KiB


2025-06-16 (Mon)
13:11:47 +09:00