結果
| 問題 |
No.416 旅行会社
|
| ユーザー |
|
| 提出日時 | 2016-11-17 11:41:34 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 567 ms / 4,000 ms |
| コード長 | 1,744 bytes |
| コンパイル時間 | 824 ms |
| コンパイル使用メモリ | 115,984 KB |
| 実行使用メモリ | 51,356 KB |
| 最終ジャッジ日時 | 2024-06-12 05:07:10 |
| 合計ジャッジ時間 | 8,743 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 |
ソースコード
import std.stdio;
import std.array;
import std.string;
import std.conv;
import std.algorithm;
import std.typecons;
import std.range;
import std.random;
import std.math;
import std.container;
import std.numeric;
struct UnionFind {
int[] n_group;
int[][] group_n;
this (int n) {
n_group = iota(n).array;
group_n = iota(n).map!(i => [i].array).array;
}
void unite(int x, int y) {
int gx = find(x);
int gy = find(y);
if (gx == gy) return;
if (group_n[gx].length < group_n[gy].length) swap(gx, gy);
foreach (ye; group_n[gy]) {
n_group[ye] = gx;
group_n[gx] ~= ye;
}
}
int find(int x) {
return n_group[x];
}
bool is_peer(int x, int y) {
return find(x) == find(y);
}
}
void main() {
auto input = readln.split.map!(to!int);
auto N = input[0];
auto M = input[1];
auto Q = input[2];
auto AB = iota(M).map!(_ => readln.split.map!(x => x.to!int-1).array).array;
auto CD = iota(Q).map!(_ => readln.split.map!(x => x.to!int-1).array).array;
bool[int][int] destroyed;
foreach (cd; CD)
destroyed[cd[0]][cd[1]] = true;
auto uf = UnionFind(N);
foreach(ab; AB) {
int a = ab[0];
int b = ab[1];
if (a in destroyed && b in destroyed[a]) continue;
uf.unite(a, b);
}
auto ans = new int[](N);
uf.group_n[uf.find(0)].each!(n => ans[n] = -1);
foreach (q; iota(Q-1, -1, -1)) {
int c = CD[q][0];
int d = CD[q][1];
if (uf.find(c) == uf.find(d))
continue;
else if (uf.find(0) == uf.find(c))
foreach (n; uf.group_n[uf.find(d)])
ans[n] = q+1;
else if (uf.find(0) == uf.find(d))
foreach (n; uf.group_n[uf.find(c)])
ans[n] = q+1;
uf.unite(c, d);
}
ans[1..$].each!(writeln);
}