結果

問題 No.1647 Travel in Mitaru city 2
ユーザー 👑 hos.lyric
提出日時 2021-08-13 22:18:22
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 227 ms / 2,500 ms
コード長 2,952 bytes
コンパイル時間 1,053 ms
コンパイル使用メモリ 132,036 KB
実行使用メモリ 22,632 KB
最終ジャッジ日時 2024-06-22 12:05:00
合計ジャッジ時間 13,134 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std
    .typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens
    .popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1;
    (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
int[] A, B;
int[][] G;
bool[] vis;
int[] pari;
int[] ans;
bool dfs(int u, int ip) {
vis[u] = true;
pari[u] = ip;
foreach (i; G[u]) {
if (i != ip) {
const v = A[i] ^ B[i] ^ u;
if (vis[v]) {
ans ~= i;
for (int w = u; w != v; ) {
const j = pari[w];
ans ~= j;
w = A[j] ^ B[j] ^ w;
}
return true;
}
if (dfs(v, i)) {
return true;
}
}
}
return false;
}
void main() {
try {
for (; ; ) {
readLong();
readLong();
const M = readInt();
long[] X = new long[M];
long[] Y = new long[M];
foreach (i; 0 .. M) {
X[i] = readLong();
Y[i] = readLong();
}
const xs = X.dup.sort.uniq.array;
const ys = Y.dup.sort.uniq.array;
const xsLen = cast(int)(xs.length);
const ysLen = cast(int)(ys.length);
A = new int[M];
B = new int[M];
foreach (i; 0 .. M) {
A[i] = xs.lowerBound(X[i]);
B[i] = xsLen + ys.lowerBound(Y[i]);
}
G = new int[][xsLen + ysLen];
foreach (i; 0 .. M) {
G[A[i]] ~= i;
G[B[i]] ~= i;
}
vis = new bool[xsLen + ysLen];
pari = new int[xsLen + ysLen];
pari[] = -1;
ans = null;
foreach (u; 0 .. xsLen + ysLen) {
if (!vis[u]) {
if (dfs(u, -1)) {
break;
}
}
}
if (ans) {
if (Y[ans[0]] != Y[ans[1]]) {
ans = ans[1 .. $] ~ ans[0];
}
writeln(ans.length);
foreach (index, i; ans) {
if (index > 0) write(" ");
write(i + 1);
}
writeln;
} else {
writeln(-1);
}
}
} catch (EOFException e) {
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0