結果

問題 No.812 Change of Class
ユーザー te-shte-sh
提出日時 2020-01-12 14:58:15
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 480 ms / 4,000 ms
コード長 2,640 bytes
コンパイル時間 1,559 ms
コンパイル使用メモリ 149,008 KB
実行使用メモリ 18,268 KB
最終ジャッジ日時 2023-09-04 04:34:54
合計ジャッジ時間 16,176 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 280 ms
13,632 KB
testcase_01 AC 43 ms
4,380 KB
testcase_02 AC 148 ms
13,632 KB
testcase_03 AC 333 ms
14,152 KB
testcase_04 AC 291 ms
13,868 KB
testcase_05 AC 471 ms
13,800 KB
testcase_06 AC 360 ms
13,352 KB
testcase_07 AC 4 ms
6,852 KB
testcase_08 AC 3 ms
6,068 KB
testcase_09 AC 445 ms
15,160 KB
testcase_10 AC 458 ms
15,100 KB
testcase_11 AC 24 ms
7,412 KB
testcase_12 AC 280 ms
18,268 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 293 ms
14,112 KB
testcase_16 AC 18 ms
4,380 KB
testcase_17 AC 268 ms
14,856 KB
testcase_18 AC 188 ms
13,852 KB
testcase_19 AC 218 ms
14,140 KB
testcase_20 AC 480 ms
14,576 KB
testcase_21 AC 181 ms
15,096 KB
testcase_22 AC 82 ms
14,104 KB
testcase_23 AC 15 ms
4,380 KB
testcase_24 AC 23 ms
4,380 KB
testcase_25 AC 67 ms
13,852 KB
testcase_26 AC 368 ms
14,612 KB
testcase_27 AC 310 ms
13,916 KB
testcase_28 AC 209 ms
14,328 KB
testcase_29 AC 52 ms
14,600 KB
testcase_30 AC 281 ms
14,788 KB
testcase_31 AC 231 ms
13,932 KB
testcase_32 AC 102 ms
14,120 KB
testcase_33 AC 298 ms
14,636 KB
testcase_34 AC 21 ms
4,380 KB
testcase_35 AC 61 ms
14,332 KB
testcase_36 AC 26 ms
4,380 KB
testcase_37 AC 152 ms
12,744 KB
testcase_38 AC 8 ms
7,120 KB
testcase_39 AC 156 ms
13,872 KB
testcase_40 AC 36 ms
4,376 KB
testcase_41 AC 7 ms
6,876 KB
testcase_42 AC 352 ms
13,592 KB
testcase_43 AC 50 ms
9,224 KB
testcase_44 AC 229 ms
14,832 KB
testcase_45 AC 26 ms
4,384 KB
testcase_46 AC 45 ms
8,692 KB
testcase_47 AC 230 ms
13,420 KB
testcase_48 AC 247 ms
14,296 KB
testcase_49 AC 67 ms
12,532 KB
testcase_50 AC 5 ms
4,376 KB
testcase_51 AC 59 ms
14,880 KB
testcase_52 AC 121 ms
15,232 KB
testcase_53 AC 43 ms
4,376 KB
testcase_54 AC 38 ms
4,380 KB
testcase_55 AC 226 ms
13,640 KB
testcase_56 AC 286 ms
13,608 KB
testcase_57 AC 296 ms
14,608 KB
testcase_58 AC 144 ms
13,848 KB
testcase_59 AC 322 ms
14,340 KB
testcase_60 AC 1 ms
4,376 KB
testcase_61 AC 1 ms
4,376 KB
testcase_62 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// URL: https://yukicoder.me/problems/no/812

import std.algorithm, std.container, std.math, std.range, std.typecons, std.string;

version(unittest) {} else
void main()
{
  int n, m; io.getV(n, m);

  auto g = Graph(n);
  foreach (_; 0..m) {
    int p, q; io.getV(p, q); --p; --q;
    g.addEdgeB(p, q);
  }

  int q; io.getV(q);
  foreach (_; 0..q) {
    int a; io.getV(a); --a;

    auto qu = DList!(g.Node)(a);
    auto md = 0, d = new int[](n), c = 0;
    d[] = -1; d[a] = 0;
    while (!qu.empty) {
      auto u = qu.front; qu.removeFront();
      ++c;

      foreach (v; g[u]) {
        if (d[v] == -1) {
          d[v] = d[u] + 1;
          md = max(md, d[v]);
          qu.insertBack(v);
        }
      }
    }
    auto x = 0;
    while (md > 1) {
      ++x;
      md = (md+1) / 2;
    }
    io.put(c-1, x);
  }
}

struct Graph
{
  alias Node = int;
  Node n;
  Node[][] g;
  alias g this;
  this(Node n) { this.n = n; g = new Node[][](n); }
  void addEdge(Node u, Node v) { g[u] ~= v; }
  void addEdgeB(Node u, Node v) { g[u] ~= v; g[v] ~= u; }
}

auto io = IO();

struct IO
{
  import std.algorithm, std.conv, std.format, std.meta, std.range, std.stdio, std.traits;

  dchar[] buf;
  auto sp = (new dchar[](0)).splitter;
  int precision = 10;
  string delimiter = " ";

  void nextLine()
  {
    stdin.readln(buf);
    sp = buf.splitter;
  }

  auto get(T)(ref T v)
  {
    if (sp.empty) nextLine();
    v = sp.front.to!T;
    sp.popFront();
  }

  auto getV(T...)(ref T v)
  {
    foreach (ref w; v) get(w);
  }

  auto getA(T)(size_t n, ref T v)
  if (hasAssignableElements!T)
  {
    v = new T(n);
    foreach (ref w; v) get(w);
  }

  auto getC(T...)(size_t n, ref T v)
  if (allSatisfy!(hasAssignableElements, T))
  {
    foreach (ref w; v)
      w = new typeof(w)(n);
    foreach (i; 0..n)
      foreach (ref w; v) get(w[i]);
  }

  auto getM(T)(size_t r, size_t c, ref T v)
  if (hasAssignableElements!T && hasAssignableElements!(ElementType!T))
  {
    v = new T(r);
    foreach (ref w; v) getA(c, w);
  }

  auto rangePop(R)(ref R r)
  {
    r.popFront();
    if (!r.empty) write(delimiter);
  }

  auto putA(T)(T v)
  {
    static if (isInputRange!T && !isSomeString!T)
      for (auto w = v; !w.empty; rangePop(w)) putA(w.front);
    else if (isFloatingPoint!T)
      writef(format("%%.%df", precision), v);
    else
      write(v);
  }

  auto put(T...)(T v)
  {
    foreach (i, w; v) {
      putA(w);
      if (i < v.length-1) write(delimiter);
    }
    writeln;
  }

  auto putB(S, T)(bool c, S t, T f)
  {
    if (c)
      put(t);
    else
      put(f);
  }

  auto dbg(T...)(T v)
  {
    stderr.writeln(v);
  }
}
0