結果

問題 No.399 動的な領主
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-21 09:55:08
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 239 ms / 2,000 ms
コード長 3,036 bytes
コンパイル時間 1,372 ms
コンパイル使用メモリ 106,640 KB
実行使用メモリ 27,012 KB
最終ジャッジ日時 2023-09-03 22:50:32
合計ジャッジ時間 5,595 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 21 ms
4,908 KB
testcase_06 AC 239 ms
18,280 KB
testcase_07 AC 236 ms
18,052 KB
testcase_08 AC 228 ms
18,112 KB
testcase_09 AC 224 ms
18,236 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 20 ms
6,140 KB
testcase_12 AC 212 ms
17,584 KB
testcase_13 AC 204 ms
17,516 KB
testcase_14 AC 177 ms
27,012 KB
testcase_15 AC 186 ms
25,756 KB
testcase_16 AC 194 ms
22,588 KB
testcase_17 AC 223 ms
18,236 KB
testcase_18 AC 222 ms
19,036 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; }

void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

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)); }


enum LIM_LOGN = 17;

int N;
int[] U, V;
int Q;
int[] A, B;

int[][] G;
int[][] par;
int[] dep;

void dfs(int u, int p, int d) {
  par[0][u] = p;
  dep[u] = d;
  foreach (v; G[u]) {
    if (v != p) {
      dfs(v, u, d + 1);
    }
  }
}

int getLCA(int u, int v) {
  foreach_reverse (e; 0 .. LIM_LOGN) {
    if (dep[u] - (1 << e) >= dep[v]) {
      u = par[e][u];
    }
    if (dep[v] - (1 << e) >= dep[u]) {
      v = par[e][v];
    }
  }
  foreach_reverse (e; 0 .. LIM_LOGN) {
    if (par[e][u] != par[e][v]) {
      u = par[e][u];
      v = par[e][v];
    }
  }
  if (u != v) {
    u = par[0][u];
    v = par[0][v];
  }
  return u;
}

long[] sum, sumL;

void dfs1(int u) {
  foreach (v; G[u]) {
    if (v != par[0][u]) {
      dfs1(v);
      sum[u] += sum[v];
    }
  }
}

void main() {
  try {
    for (; ; ) {
      N = readInt();
      U = new int[N - 1];
      V = new int[N - 1];
      foreach (i; 0 .. N - 1) {
        U[i] = readInt() - 1;
        V[i] = readInt() - 1;
      }
      Q = readInt();
      A = new int[Q];
      B = new int[Q];
      foreach (q; 0 .. Q) {
        A[q] = readInt() - 1;
        B[q] = readInt() - 1;
      }
      
      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[U[i]] ~= V[i];
        G[V[i]] ~= U[i];
      }
      par = new int[][](LIM_LOGN, N);
      dep = new int[N];
      const rt = 0;
      dfs(rt, rt, 0);
      foreach (e; 1 .. LIM_LOGN) {
        foreach (u; 0 .. N) {
          par[e][u] = par[e - 1][par[e - 1][u]];
        }
      }
      
      sum = new long[N];
      sumL = new long[N];
      foreach (q; 0 .. Q) {
        const l = getLCA(A[q], B[q]);
        sum[A[q]] += 1;
        sum[B[q]] += 1;
        sum[l] -= 2;
        sumL[l] += 1;
      }
      dfs1(rt);
      long ans;
      foreach (u; 0 .. N) {
        const c = sum[u] + sumL[u];
        ans += c * (c + 1) / 2;
        debug {
          writeln(u, " ", c);
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0