結果

問題 No.768 Tapris and Noel play the game on Treeone
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-24 23:32:50
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 143 ms / 2,000 ms
コード長 2,779 bytes
コンパイル時間 919 ms
コンパイル使用メモリ 106,776 KB
実行使用メモリ 9,744 KB
最終ジャッジ日時 2023-09-03 23:03:44
合計ジャッジ時間 4,961 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,504 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 82 ms
6,992 KB
testcase_08 AC 42 ms
4,872 KB
testcase_09 AC 50 ms
5,888 KB
testcase_10 AC 38 ms
6,024 KB
testcase_11 AC 131 ms
9,744 KB
testcase_12 AC 128 ms
8,492 KB
testcase_13 AC 131 ms
8,024 KB
testcase_14 AC 130 ms
9,472 KB
testcase_15 AC 135 ms
8,312 KB
testcase_16 AC 139 ms
8,292 KB
testcase_17 AC 143 ms
8,552 KB
testcase_18 AC 131 ms
8,848 KB
testcase_19 AC 134 ms
9,044 KB
testcase_20 AC 135 ms
9,080 KB
testcase_21 AC 129 ms
8,544 KB
20evil_special_uni1.txt AC 131 ms
8,540 KB
20evil_special_uni2.txt AC 126 ms
8,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, 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(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); }


int N;
int[] A, B;

int[][] G;
int[] par;
int[] dp, DP;

void dfs(int u, int p) {
  par[u] = p;
  foreach (v; G[u]) {
    if (v != p) {
      dfs(v, u);
    }
  }
  int sum;
  foreach (v; G[u]) {
    if (v != p) {
      sum += dp[v];
    }
  }
  dp[u] = (sum == 0) ? 1 : 0;
}

void DFS(int u, int p) {
  int sum;
  foreach (v; G[u]) {
    if (v != p) {
      sum += dp[v];
    }
  }
  if (p != -1) {
    sum += DP[u];
  }
  foreach (v; G[u]) {
    if (v != p) {
      debug {
        writefln("%s -> %s: %s", v, u, sum - dp[v]);
      }
      DP[v] = (sum - dp[v] == 0) ? 1 : 0;
    }
  }
  foreach (v; G[u]) {
    if (v != p) {
      DFS(v, u);
    }
  }
}

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]] ~= B[i];
        G[B[i]] ~= A[i];
      }
      par = new int[N];
      dp = new int[N];
      DP = new int[N];
      const rt = 0;
      dfs(rt, -1);
      DFS(rt, -1);
      debug {
        writeln("rt = ", rt);
        writeln("dp = ", dp);
        writeln("DP = ", DP);
      }
      int[] ans;
      foreach (u; 0 .. N) {
        int sum;
        foreach (v; G[u]) {
          if (v != par[u]) {
            sum += dp[v];
          }
        }
        if (u != rt) {
          sum += DP[u];
        }
        if (sum == 0) {
          ans ~= u;
        }
      }
      writeln(ans.length);
      foreach (u; ans) {
        writeln(u + 1);
      }
    }
  } catch (EOFException e) {
  }
}
0