結果
| 問題 |
No.1647 Travel in Mitaru city 2
|
| コンテスト | |
| ユーザー |
puzneko
|
| 提出日時 | 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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 48 |
ソースコード
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))
puzneko