結果

問題 No.812 Change of Class
ユーザー nebukuro09nebukuro09
提出日時 2019-04-12 21:54:11
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,342 ms / 4,000 ms
コード長 1,715 bytes
コンパイル時間 761 ms
コンパイル使用メモリ 120,980 KB
実行使用メモリ 35,548 KB
最終ジャッジ日時 2023-09-04 00:36:21
合計ジャッジ時間 28,364 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 354 ms
13,548 KB
testcase_01 AC 56 ms
4,376 KB
testcase_02 AC 196 ms
9,068 KB
testcase_03 AC 420 ms
13,560 KB
testcase_04 AC 353 ms
13,524 KB
testcase_05 AC 1,342 ms
20,948 KB
testcase_06 AC 1,328 ms
22,548 KB
testcase_07 AC 5 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 1,099 ms
17,260 KB
testcase_10 AC 1,150 ms
17,632 KB
testcase_11 AC 31 ms
6,276 KB
testcase_12 AC 627 ms
15,400 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 812 ms
15,620 KB
testcase_16 AC 44 ms
4,380 KB
testcase_17 AC 698 ms
15,896 KB
testcase_18 AC 530 ms
14,792 KB
testcase_19 AC 616 ms
14,780 KB
testcase_20 AC 1,197 ms
18,572 KB
testcase_21 AC 514 ms
13,840 KB
testcase_22 AC 199 ms
5,668 KB
testcase_23 AC 33 ms
4,376 KB
testcase_24 AC 54 ms
4,380 KB
testcase_25 AC 158 ms
5,036 KB
testcase_26 AC 951 ms
16,640 KB
testcase_27 AC 852 ms
16,976 KB
testcase_28 AC 564 ms
14,640 KB
testcase_29 AC 122 ms
4,808 KB
testcase_30 AC 764 ms
16,644 KB
testcase_31 AC 668 ms
15,000 KB
testcase_32 AC 259 ms
8,720 KB
testcase_33 AC 784 ms
15,680 KB
testcase_34 AC 50 ms
4,380 KB
testcase_35 AC 108 ms
4,376 KB
testcase_36 AC 55 ms
4,376 KB
testcase_37 AC 335 ms
7,068 KB
testcase_38 AC 12 ms
4,544 KB
testcase_39 AC 355 ms
7,532 KB
testcase_40 AC 70 ms
4,376 KB
testcase_41 AC 9 ms
4,380 KB
testcase_42 AC 838 ms
13,552 KB
testcase_43 AC 81 ms
10,124 KB
testcase_44 AC 548 ms
10,184 KB
testcase_45 AC 56 ms
4,380 KB
testcase_46 AC 69 ms
9,644 KB
testcase_47 AC 545 ms
13,004 KB
testcase_48 AC 548 ms
13,296 KB
testcase_49 AC 144 ms
4,376 KB
testcase_50 AC 9 ms
4,376 KB
testcase_51 AC 116 ms
4,456 KB
testcase_52 AC 237 ms
10,732 KB
testcase_53 AC 91 ms
4,376 KB
testcase_54 AC 83 ms
4,376 KB
testcase_55 AC 617 ms
28,372 KB
testcase_56 AC 743 ms
31,728 KB
testcase_57 AC 781 ms
33,212 KB
testcase_58 AC 399 ms
17,864 KB
testcase_59 AC 909 ms
35,548 KB
testcase_60 AC 2 ms
4,380 KB
testcase_61 AC 1 ms
4,376 KB
testcase_62 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

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 = d <= 1 ? 0 : bsr(d-1) + 1;
        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