結果
| 問題 |
No.416 旅行会社
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-10-26 10:59:16 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,074 bytes |
| コンパイル時間 | 928 ms |
| コンパイル使用メモリ | 113,044 KB |
| 実行使用メモリ | 25,844 KB |
| 最終ジャッジ日時 | 2024-06-12 22:07:41 |
| 合計ジャッジ時間 | 8,380 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 5 WA * 6 TLE * 1 -- * 9 |
ソースコード
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split.to!(int[]), n = rd[0], m = rd[1], q = rd[2];
auto e1 = loadEdges(m), e2 = loadEdges(q);
auto uf = UnionFind!int(n);
auto e2s = e2.dup.sort!("a.u == b.u ? a.v < b.v : a.u < b.u");
foreach (ei; e1.filter!(ei => !e2s.contains(ei))) {
uf.unite(ei.u, ei.v);
}
auto ans = new int[](n);
ans[] = -1;
foreach_reverse (i, ei; e2) {
if (uf[ei.u] == uf[0] && uf[ei.v] != uf[0])
foreach (v; uf.nodes[uf[ei.v]]) ans[v] = i.to!int+1;
if (uf[ei.v] == uf[0] && uf[ei.u] != uf[0])
foreach (u; uf.nodes[uf[ei.u]]) ans[u] = i.to!int+1;
uf.unite(ei.u, ei.v);
}
foreach (i; 1..n) writeln(ans[i]);
}
struct Edge { int u, v; }
auto loadEdges(size_t m)
{
auto e = new Edge[](m);
foreach (i; 0..m) {
auto rd = readln.splitter;
auto a = rd.front.to!int-1; rd.popFront();
auto b = rd.front.to!int-1;
e[i] = Edge(a, b);
}
return e;
}
struct UnionFind(T)
{
import std.algorithm, std.range;
T[] p; // parent
const T s; // sentinel
const T n;
T countForests; // number of forests
T[] countNodes; // number of nodes in forests
T[][] nodes;
this(T n)
{
this.n = n;
s = n;
p = new T[](n);
p[] = s;
countForests = n;
countNodes = new T[](n);
countNodes[] = 1;
nodes = new T[][](n);
foreach (i; 0..n) nodes[i] = [i];
}
T opIndex(T i)
{
if (p[i] == s) {
return i;
} else {
p[i] = this[p[i]];
return p[i];
}
}
bool unite(T i, T j)
{
auto pi = this[i], pj = this[j];
if (pi != pj) {
p[pj] = pi;
--countForests;
countNodes[pi] += countNodes[pj];
nodes[pi] ~= nodes[pj];
nodes[pj] = [];
return true;
} else {
return false;
}
}
auto countNodesOf(T i) { return countNodes[this[i]]; }
bool isSame(T i, T j) { return this[i] == this[j]; }
auto groups()
{
auto g = new T[][](n);
foreach (i; 0..n) g[this[i]] ~= i;
return g.filter!(l => !l.empty);
}
}