結果

問題 No.1647 Travel in Mitaru city 2
ユーザー puznekopuzneko
提出日時 2021-10-25 00:40:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,062 ms / 2,500 ms
コード長 3,169 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 142,648 KB
最終ジャッジ日時 2024-10-02 07:42:23
合計ジャッジ時間 45,234 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
52,736 KB
testcase_01 AC 42 ms
52,992 KB
testcase_02 AC 43 ms
52,992 KB
testcase_03 AC 44 ms
53,248 KB
testcase_04 AC 68 ms
67,456 KB
testcase_05 AC 45 ms
53,120 KB
testcase_06 AC 44 ms
53,760 KB
testcase_07 AC 68 ms
69,376 KB
testcase_08 AC 1,010 ms
134,944 KB
testcase_09 AC 921 ms
118,424 KB
testcase_10 AC 1,052 ms
137,828 KB
testcase_11 AC 909 ms
114,308 KB
testcase_12 AC 979 ms
122,336 KB
testcase_13 AC 945 ms
124,396 KB
testcase_14 AC 1,013 ms
131,680 KB
testcase_15 AC 1,009 ms
130,592 KB
testcase_16 AC 923 ms
126,524 KB
testcase_17 AC 909 ms
117,148 KB
testcase_18 AC 1,025 ms
127,672 KB
testcase_19 AC 962 ms
117,680 KB
testcase_20 AC 1,046 ms
127,664 KB
testcase_21 AC 1,011 ms
123,300 KB
testcase_22 AC 1,056 ms
132,788 KB
testcase_23 AC 1,025 ms
131,240 KB
testcase_24 AC 1,017 ms
130,812 KB
testcase_25 AC 1,003 ms
121,008 KB
testcase_26 AC 1,037 ms
133,140 KB
testcase_27 AC 1,061 ms
133,564 KB
testcase_28 AC 1,019 ms
142,200 KB
testcase_29 AC 1,051 ms
142,648 KB
testcase_30 AC 973 ms
119,348 KB
testcase_31 AC 1,010 ms
138,676 KB
testcase_32 AC 978 ms
124,612 KB
testcase_33 AC 1,020 ms
131,384 KB
testcase_34 AC 1,045 ms
142,516 KB
testcase_35 AC 956 ms
120,104 KB
testcase_36 AC 1,027 ms
142,260 KB
testcase_37 AC 987 ms
138,292 KB
testcase_38 AC 1,046 ms
132,272 KB
testcase_39 AC 1,062 ms
122,040 KB
testcase_40 AC 1,019 ms
131,764 KB
testcase_41 AC 1,005 ms
118,852 KB
testcase_42 AC 1,053 ms
131,648 KB
testcase_43 AC 42 ms
53,376 KB
testcase_44 AC 50 ms
63,232 KB
testcase_45 AC 721 ms
113,932 KB
testcase_46 AC 850 ms
116,440 KB
testcase_47 AC 802 ms
115,732 KB
testcase_48 AC 853 ms
117,164 KB
testcase_49 AC 676 ms
112,384 KB
testcase_50 AC 618 ms
112,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin
from heapq import heappush, heappop
from sys import setrecursionlimit
setrecursionlimit(10000000)

w, h, n, *indata = map(int, stdin.read().split())
offset = 0
hq = []
xylist = [(-1,-1)]
for i in range(n):
    y, x = indata[offset + 2*i],indata[offset + 2*i+1]
    heappush(hq, (y, x, i+1))
    xylist.append((x,y))

par = [i for i in range(h+1)]
rank = [0 for i in range(h+1)]

def root(x):
    if par[x] == x:
        return x
    else:
        par[x] = root(par[x])
        return par[x]

def unite(x,y):
    karix = root(x)
    kariy = root(y)
    if karix == kariy:
        return 0
    if rank[karix] < rank[kariy]:
        par[karix] = kariy
    else:
        par[kariy] = karix
        if rank[karix] == rank[kariy]:
            rank[karix] += 1

def same(x,y):
    return root(x) == root(y)

prey = -1
prex = -1
preind = -1
samexlist = [[] for j in range(h+1)]
g = [[] for i in range(n+1)]
while hq:
    y, x, ind = heappop(hq)
    if prey == y:
        if same(prex,x):
            g[preind].append(ind)
            ind2 = samexlist[x][-1]
            g[ind2].append(ind)
            g[ind].append(ind2)
            que = [(ind,0)]
            route = [ind]
            check = [False for i in range(n+1)]
            check[ind] = True
            while que:
                now, inout = que.pop()
                if now == preind:
                    route.append(now)
                    route.append(ind)
                    ans = [ind]
                    lenroute = len(route)
                    parity = 0
                    for i in range(1,lenroute):
                        if parity:
                            if xylist[ans[-1]][0] == xylist[route[i]][0]:
                                ans.pop()
                                ans.append(route[i])
                            else:
                                ans.append(route[i])
                                parity = parity ^ 1
                        else:
                            if xylist[ans[-1]][1] == xylist[route[i]][1]:
                                ans.pop()
                                ans.append(route[i])
                            else:
                                ans.append(route[i])
                                parity = parity ^ 1
                    ans.pop()
                    ansx = len(ans)
                    print("{}".format(ansx))
                    L=[str(i) for i in ans]
                    L=' '.join(L)
                    print(L)
                    exit()
                if inout == 0:
                    que.append((now,1))
                    route.append(now)
                    for i in g[now]:
                        if not check[i]:
                            check[i] = True
                            que.append((i,0))
                else:
                    route.pop()
        else:
            unite(prex,x)
        g[ind].append(preind)
        g[preind].append(ind)
    else:
        prey = y
    if samexlist[x]:
        ind2 = samexlist[x][-1]
        g[ind2].append(ind)
        g[ind].append(ind2)
    samexlist[x].append(ind)
    prex = x
    preind = ind

print("{}".format(-1))
0