結果

問題 No.812 Change of Class
ユーザー nebukuro09nebukuro09
提出日時 2019-04-12 21:41:14
言語 D
(dmd 2.105.2)
結果
WA  
実行時間 -
コード長 1,773 bytes
コンパイル時間 841 ms
コンパイル使用メモリ 122,824 KB
実行使用メモリ 35,492 KB
最終ジャッジ日時 2023-09-04 00:32:44
合計ジャッジ時間 26,375 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 5 ms
4,380 KB
testcase_08 AC 3 ms
4,384 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 30 ms
6,264 KB
testcase_12 WA -
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 12 ms
4,416 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 9 ms
4,380 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 AC 602 ms
27,492 KB
testcase_56 AC 728 ms
32,236 KB
testcase_57 AC 775 ms
32,812 KB
testcase_58 AC 389 ms
19,424 KB
testcase_59 AC 891 ms
35,492 KB
testcase_60 AC 1 ms
4,376 KB
testcase_61 AC 1 ms
4,380 KB
testcase_62 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto G = new int[][](N);
    auto uf = new UnionFind(N);
    foreach (_; 0..M) {
        s = readln.split.map!(to!int);
        G[s[0]-1] ~= s[1]-1;
        G[s[1]-1] ~= s[0]-1;
        uf.unite(s[0]-1, s[1]-1);
    }

    auto Q = readln.chomp.to!int;
    auto dist = new int[](N);

    while (Q--) {
        auto a = readln.chomp.to!int - 1;
        dist[] = 1 << 29;
        auto pq = new BinaryHeap!(Array!(Tuple!(int, int)), "a[1] > b[1]");
        pq.insert(tuple(a, 0));

        while (!pq.empty) {
            auto n = pq.front[0];
            auto d = pq.front[1];
            pq.removeFront;
            if (dist[n] <= d) continue;
            dist[n] = d;
            foreach (m; G[n]) {
                if (dist[m] <= d + 1) continue;
                pq.insert(tuple(m, d+1));
            }
        }

        auto d = dist.filter!(x => x != 1 << 29).reduce!max;
        int ans;
        if (d <= 1) ans = 0;
        else if (d == 2) ans = 1;
        else ans = 2;

        writeln(-uf.table[uf.find(a)]-1, " ", ans);
    }
}

class UnionFind {
    int N;
    int[] table;

    this(int n) {
        N = n;
        table = new int[](N);
        fill(table, -1);
    }

    int find(int x) {
        return table[x] < 0 ? x : (table[x] = find(table[x]));
    }

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (table[x] > table[y]) swap(x, y);
        table[x] += table[y];
        table[y] = x;
    }
}
0