結果

問題 No.416 旅行会社
ユーザー noriocnorioc
提出日時 2018-01-14 11:44:16
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 513 ms / 4,000 ms
コード長 2,890 bytes
コンパイル時間 740 ms
コンパイル使用メモリ 96,136 KB
実行使用メモリ 32,160 KB
最終ジャッジ日時 2023-09-03 18:10:25
合計ジャッジ時間 7,431 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 250 ms
18,432 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 7 ms
4,376 KB
testcase_09 AC 28 ms
4,652 KB
testcase_10 AC 251 ms
19,020 KB
testcase_11 AC 249 ms
17,516 KB
testcase_12 AC 248 ms
18,188 KB
testcase_13 AC 245 ms
18,096 KB
testcase_14 AC 513 ms
30,868 KB
testcase_15 AC 510 ms
31,372 KB
testcase_16 AC 512 ms
31,888 KB
testcase_17 AC 511 ms
31,676 KB
testcase_18 AC 509 ms
32,160 KB
testcase_19 AC 306 ms
20,632 KB
testcase_20 AC 303 ms
21,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

int readint() { return readln.chomp.to!int; }
int[] readints() { return readln.split.to!(int[]); }

alias Pair = Tuple!(int, "a", int, "b");

Pair readPair() {
    auto xs = readints;
    return Pair(xs[0], xs[1]);
}

void main() {
    auto nmq = readints;
    int n = nmq[0], m = nmq[1], q = nmq[2];

    Pair[] pairs;
    for (int i = 0; i < m; i++) {
        pairs ~= readPair();
    }

    bool[Pair] breakMap;
    Pair[] breaks;
    for (int i = 0; i < q; i++) {
        auto p = readPair();
        breaks ~= p;
        breakMap[p] = true;
    }

    auto uf = new UnionFind(n);
    foreach (p; pairs) {
        // 破壊に関係ない橋を予め接続しておく
        if (p !in breakMap) {
            uf.unite(p.a, p.b);
        }
    }

    auto ans = new int[](n + 1);
    ans[] = -1; // 全て到達できる
    foreach_reverse (i, brk; breaks) {
        const ra = uf.root(brk.a);
        const rb = uf.root(brk.b);
        const r1 = uf.root(1);
        if (ra != rb && (ra == r1 || rb == r1)) {
            int nth = cast(int) i + 1;
            int node = ra == r1 ? rb : ra; // 1 と接続された集合のルート
            uf.doEach(node, delegate(e) { ans[e] = nth; });
        }

        uf.unite(brk.a, brk.b);
    }

    for (int i = 2; i <= n; i++) {
        if (uf.isSame(1, i))
            writeln(ans[i]);
        else
            writeln(0); // 最初から到達できない
    }
}

class UnionFind {
    private int[] _data;
    private int[] _nexts; // 次の要素。なければ -1
    private int[] _tails; // 末尾の要素

    this(int n) {
        _data  = new int[](n + 1);
        _nexts = new int[](n + 1);
        _tails = new int[](n + 1);

        _data[] = -1;
        _nexts[] = -1;
        for (int i = 0; i < _tails.length; i++)
            _tails[i] = i;
    }

    int root(int a) {
        if (_data[a] < 0) return a;
        return _data[a] = root(_data[a]);
    }

    bool unite(int a, int b) {
        int ra = root(a);
        int rb = root(b);
        if (ra == rb) return false;
        // ra に rb を繋げる
        _data[ra] += _data[rb];
        _data[rb] = ra;

        // ra の末尾の後ろに rb を繋げる
        _nexts[_tails[ra]] = rb;
        // ra の末尾が rb の末尾になる
        _tails[ra] = _tails[rb];
        return true;
    }

    bool isSame(int a, int b) {
        return root(a) == root(b);
    }

    /// a が属する集合のサイズ
    int groupSize(int a) {
        return -_data[root(a)];
    }

    /// a が属する集合の要素全て
    void doEach(int a, void delegate(int) fn) {
        auto node = root(a);
        while (node != -1) {
            fn(node);
            node = _nexts[node];
        }
    }
}
0