結果

問題 No.416 旅行会社
ユーザー noriocnorioc
提出日時 2017-12-29 19:12:47
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 411 ms / 4,000 ms
コード長 3,058 bytes
コンパイル時間 1,015 ms
コンパイル使用メモリ 98,372 KB
実行使用メモリ 31,852 KB
最終ジャッジ日時 2023-09-03 17:51:48
合計ジャッジ時間 6,672 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 200 ms
17,800 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 6 ms
4,380 KB
testcase_09 AC 24 ms
4,564 KB
testcase_10 AC 202 ms
18,332 KB
testcase_11 AC 195 ms
17,916 KB
testcase_12 AC 195 ms
18,340 KB
testcase_13 AC 191 ms
17,528 KB
testcase_14 AC 401 ms
30,888 KB
testcase_15 AC 394 ms
31,852 KB
testcase_16 AC 392 ms
31,356 KB
testcase_17 AC 400 ms
31,628 KB
testcase_18 AC 411 ms
31,444 KB
testcase_19 AC 246 ms
21,816 KB
testcase_20 AC 241 ms
22,148 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.map!(to!int).array; }

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

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

    Pair[] pairs;
    for (int i = 0; i < m; i++) {
        auto ab = readints;
        pairs ~= Pair(ab[0], ab[1]);
    }

    bool[Pair] breakMap;
    Pair[] breaks;
    for (int i = 0; i < q; i++) {
        auto cd = readints;
        auto p = Pair(cd[0], cd[1]);
        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 rootA = uf.root(brk.a);
        const rootB = uf.root(brk.b);
        const root1 = uf.root(1);
        if (rootA != rootB && (rootA == root1 || rootB == root1)) {
            int nth = cast(int) i + 1;
            int node = rootA == root1 ? rootB : rootA; // 1 と接続された集合のルート
            foreach (x; uf.group(node)) {
                ans[x] = 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 SList {
    public int node;
    public SList next;
    public SList tail;

    this(int node) {
        this.node = node;
        this.next = null;
        this.tail = this;
    }

    /// 引数 list を破壊的に変更する
    void append(SList list) {
        this.tail.next = list;
        this.tail = list.tail;
    }

    int[] dump() {
        int[] buf = [node];
        auto p = this.next;
        while (p !is null) {
            buf ~= p.node;
            p = p.next;
        }
        return buf;
    }
}

class UnionFind {
    private int[] _data;
    private SList[] _groups;

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

        _groups = new SList[](n + 1);
        for (int i = 0; i < n + 1; i++)
            _groups[i] = new SList(i);
    }

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

    bool unite(int a, int b) {
        int rootA = root(a);
        int rootB = root(b);
        if (rootA == rootB)
            return false;
        _data[rootA] += _data[rootB];
        _data[rootB] = rootA;

        _groups[rootA].append(_groups[rootB]);
        return true;
    }

    int[] group(int a) {
        return _groups[root(a)].dump();
    }

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

    int size(int a) {
        return -_data[root(a)];
    }
}
0