結果

問題 No.1928 Make a Binary Tree
ユーザー 👑 hos.lyrichos.lyric
提出日時 2022-05-06 23:43:47
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 492 ms / 3,000 ms
コード長 2,118 bytes
コンパイル時間 2,440 ms
コンパイル使用メモリ 159,576 KB
実行使用メモリ 62,360 KB
最終ジャッジ日時 2023-09-04 17:02:55
合計ジャッジ時間 17,994 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 18 ms
4,896 KB
testcase_16 AC 14 ms
4,704 KB
testcase_17 AC 9 ms
4,380 KB
testcase_18 AC 17 ms
4,672 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 10 ms
4,380 KB
testcase_21 AC 6 ms
4,380 KB
testcase_22 AC 13 ms
4,380 KB
testcase_23 AC 9 ms
4,380 KB
testcase_24 AC 16 ms
4,948 KB
testcase_25 AC 19 ms
4,888 KB
testcase_26 AC 430 ms
38,568 KB
testcase_27 AC 217 ms
24,236 KB
testcase_28 AC 284 ms
26,348 KB
testcase_29 AC 485 ms
44,728 KB
testcase_30 AC 199 ms
23,080 KB
testcase_31 AC 329 ms
29,988 KB
testcase_32 AC 475 ms
42,516 KB
testcase_33 AC 221 ms
24,464 KB
testcase_34 AC 332 ms
29,988 KB
testcase_35 AC 238 ms
34,008 KB
testcase_36 AC 351 ms
55,028 KB
testcase_37 AC 385 ms
53,696 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 375 ms
52,236 KB
testcase_40 AC 378 ms
55,800 KB
testcase_41 AC 376 ms
52,604 KB
testcase_42 AC 380 ms
54,644 KB
testcase_43 AC 375 ms
52,708 KB
testcase_44 AC 381 ms
56,292 KB
testcase_45 AC 374 ms
55,524 KB
testcase_46 AC 375 ms
55,076 KB
testcase_47 AC 373 ms
53,964 KB
testcase_48 AC 373 ms
55,316 KB
testcase_49 AC 286 ms
62,360 KB
testcase_50 AC 433 ms
39,076 KB
testcase_51 AC 438 ms
38,968 KB
testcase_52 AC 446 ms
35,932 KB
testcase_53 AC 431 ms
37,964 KB
testcase_54 AC 437 ms
39,128 KB
testcase_55 AC 308 ms
53,972 KB
testcase_56 AC 486 ms
45,540 KB
testcase_57 AC 487 ms
43,104 KB
testcase_58 AC 492 ms
43,068 KB
testcase_59 AC 414 ms
44,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


int N;
int[] A, B;

int[][] G;

alias Set = RedBlackTree!(int, "a < b", true);
Set[] dp;

void dfs(int u, int p) {
  dp[u] = new Set;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      dfs(v, u);
      if (dp[u].length < dp[v].length) {
        swap(dp[u], dp[v]);
      }
      foreach (x; dp[v]) {
        dp[u].insert(x);
      }
    }
  }
  int x = 1;
  foreach (_; 0 .. 2) {
    if (!dp[u].empty) {
      x += dp[u].back;
      dp[u].removeBack;
    }
  }
  dp[u].insert(x);
}

void main() {
  try {
    for (; ; ) {
      N = readInt;
      A = new int[N - 1];
      B = new int[N - 1];
      foreach (i; 0 .. N - 1) {
        A[i] = readInt - 1;
        B[i] = readInt - 1;
      }
      
      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }
      
      dp = new Set[N];
      dfs(0, -1);
      writeln(dp[0].back);
    }
  } catch (EOFException e) {
  }
}
0