Submission #38811433


Source Code Expand

from collections import defaultdict
import heapq
T,=map(int,input().split())

K = 2001

def solve():
    N,M=map(int,input().split())
    cl = list(map(int, input().split()))

    edges = defaultdict(list)
    for _ in range(M):
        u, v = map(int,input().split())
        edges[u-1].append(v-1)
        edges[v-1].append(u-1)
    
    costs = [100000000]*(N*K)
    costs[0+(N-1)*K]=0
    queue = []
    visited = set()
    heapq.heappush(queue, (0, 0+(N-1)*K))
    while len(queue) > 0:
        cost, pos = heapq.heappop(queue)
        if pos in visited:
            continue
        visited.add(pos)
        pos0 = pos % K
        pos1 = pos // K
        for pos0t in edges[pos0]:
            for pos1t in edges[pos1]:
                if cl[pos0t] != cl[pos1t]:
                    newcost = cost + 1
                    key = pos0t + pos1t*K
                    lastcost = costs[key]
                    if newcost < lastcost:
                        costs[key] = newcost
                        heapq.heappush(queue, (newcost, key))
    result_cost = costs[N-1+0*K]
    if result_cost >= 100000000:
        result_cost = -1
    print(result_cost)

for _ in range(T):
    solve()

Submission Info

Submission Time
Task E - Swap Places
User bugtori
Language Python (3.8.2)
Score 0
Code Size 1231 Byte
Status TLE
Exec Time 2210 ms
Memory 139784 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 1
AC × 62
TLE × 2
Set Name Test Cases
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 01_small_03.txt, 01_small_04.txt, 01_small_05.txt, 01_small_06.txt, 01_small_07.txt, 01_small_08.txt, 01_small_09.txt, 01_small_10.txt, 01_small_11.txt, 01_small_12.txt, 01_small_13.txt, 01_small_14.txt, 01_small_15.txt, 01_small_16.txt, 01_small_17.txt, 01_small_18.txt, 01_small_19.txt, 01_small_20.txt, 01_small_21.txt, 01_small_22.txt, 01_small_23.txt, 01_small_24.txt, 01_small_25.txt, 01_small_26.txt, 01_small_27.txt, 01_small_28.txt, 01_small_29.txt, 01_small_30.txt, 01_small_31.txt, 02_tree_00.txt, 02_tree_01.txt, 02_tree_02.txt, 02_tree_03.txt, 02_tree_04.txt, 02_tree_05.txt, 03_path_00.txt, 03_path_01.txt, 03_path_02.txt, 03_path_03.txt, 04_dense_00.txt, 04_dense_01.txt, 04_dense_02.txt, 04_dense_03.txt, 05_sparse_00.txt, 05_sparse_01.txt, 05_sparse_02.txt, 05_sparse_03.txt, 06_large_00.txt, 06_large_01.txt, 06_large_02.txt, 06_large_03.txt, 06_large_04.txt, 06_large_05.txt, 06_large_06.txt, 06_large_07.txt, 06_large_08.txt, 06_large_09.txt, 07_bridge_connected_00.txt, 07_bridge_connected_01.txt, 07_bridge_connected_02.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 25 ms 9196 KiB
01_small_00.txt AC 34 ms 9360 KiB
01_small_01.txt AC 19 ms 9356 KiB
01_small_02.txt AC 44 ms 9384 KiB
01_small_03.txt AC 34 ms 9364 KiB
01_small_04.txt AC 31 ms 9352 KiB
01_small_05.txt AC 37 ms 9524 KiB
01_small_06.txt AC 33 ms 9404 KiB
01_small_07.txt AC 38 ms 9508 KiB
01_small_08.txt AC 43 ms 9288 KiB
01_small_09.txt AC 39 ms 9460 KiB
01_small_10.txt AC 35 ms 9284 KiB
01_small_11.txt AC 40 ms 9412 KiB
01_small_12.txt AC 36 ms 9284 KiB
01_small_13.txt AC 43 ms 9404 KiB
01_small_14.txt AC 41 ms 9480 KiB
01_small_15.txt AC 43 ms 9284 KiB
01_small_16.txt AC 43 ms 9456 KiB
01_small_17.txt AC 45 ms 9320 KiB
01_small_18.txt AC 44 ms 9396 KiB
01_small_19.txt AC 42 ms 9444 KiB
01_small_20.txt AC 44 ms 9284 KiB
01_small_21.txt AC 41 ms 9276 KiB
01_small_22.txt AC 138 ms 10188 KiB
01_small_23.txt AC 93 ms 10556 KiB
01_small_24.txt AC 162 ms 10196 KiB
01_small_25.txt AC 175 ms 10872 KiB
01_small_26.txt AC 246 ms 10460 KiB
01_small_27.txt AC 92 ms 11180 KiB
01_small_28.txt AC 176 ms 10180 KiB
01_small_29.txt AC 157 ms 10752 KiB
01_small_30.txt AC 146 ms 10232 KiB
01_small_31.txt AC 174 ms 10860 KiB
02_tree_00.txt TLE 2209 ms 114988 KiB
02_tree_01.txt AC 52 ms 40892 KiB
02_tree_02.txt AC 52 ms 40940 KiB
02_tree_03.txt AC 173 ms 44484 KiB
02_tree_04.txt AC 53 ms 40964 KiB
02_tree_05.txt AC 233 ms 49572 KiB
03_path_00.txt TLE 2210 ms 139784 KiB
03_path_01.txt AC 54 ms 40972 KiB
03_path_02.txt AC 51 ms 40892 KiB
03_path_03.txt AC 55 ms 41080 KiB
04_dense_00.txt AC 793 ms 10376 KiB
04_dense_01.txt AC 798 ms 10208 KiB
04_dense_02.txt AC 720 ms 10348 KiB
04_dense_03.txt AC 722 ms 10320 KiB
05_sparse_00.txt AC 1526 ms 81128 KiB
05_sparse_01.txt AC 60 ms 41036 KiB
05_sparse_02.txt AC 50 ms 40808 KiB
05_sparse_03.txt AC 54 ms 40812 KiB
06_large_00.txt AC 1615 ms 136564 KiB
06_large_01.txt AC 1630 ms 136588 KiB
06_large_02.txt AC 1619 ms 136784 KiB
06_large_03.txt AC 1627 ms 136732 KiB
06_large_04.txt AC 1600 ms 136644 KiB
06_large_05.txt AC 1600 ms 136868 KiB
06_large_06.txt AC 1621 ms 137068 KiB
06_large_07.txt AC 1637 ms 137060 KiB
06_large_08.txt AC 1615 ms 136848 KiB
06_large_09.txt AC 1623 ms 136776 KiB
07_bridge_connected_00.txt AC 1272 ms 84156 KiB
07_bridge_connected_01.txt AC 1284 ms 84016 KiB
07_bridge_connected_02.txt AC 45 ms 33040 KiB