結果

問題 No.901 K-ary εxtrεεmε
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-10-04 22:20:05
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 513 ms / 3,000 ms
コード長 3,804 bytes
コンパイル時間 1,086 ms
コンパイル使用メモリ 120,256 KB
実行使用メモリ 51,748 KB
最終ジャッジ日時 2023-09-04 02:54:22
合計ジャッジ時間 15,223 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 316 ms
47,484 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 8 ms
4,376 KB
testcase_03 AC 8 ms
4,380 KB
testcase_04 AC 8 ms
4,380 KB
testcase_05 AC 9 ms
4,380 KB
testcase_06 AC 8 ms
4,380 KB
testcase_07 AC 391 ms
36,804 KB
testcase_08 AC 388 ms
36,848 KB
testcase_09 AC 387 ms
36,860 KB
testcase_10 AC 387 ms
36,804 KB
testcase_11 AC 382 ms
36,852 KB
testcase_12 AC 445 ms
36,864 KB
testcase_13 AC 458 ms
38,196 KB
testcase_14 AC 446 ms
37,108 KB
testcase_15 AC 441 ms
37,116 KB
testcase_16 AC 453 ms
37,116 KB
testcase_17 AC 380 ms
37,628 KB
testcase_18 AC 384 ms
37,612 KB
testcase_19 AC 376 ms
37,604 KB
testcase_20 AC 375 ms
37,640 KB
testcase_21 AC 379 ms
37,692 KB
testcase_22 AC 490 ms
41,208 KB
testcase_23 AC 513 ms
41,088 KB
testcase_24 AC 487 ms
41,184 KB
testcase_25 AC 483 ms
41,692 KB
testcase_26 AC 487 ms
41,216 KB
testcase_27 AC 329 ms
51,748 KB
testcase_28 AC 333 ms
50,396 KB
testcase_29 AC 348 ms
41,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, 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)); }


enum E = 18;

int N;
int[] U, V;
long[] W;
int Q;
int[] K;
int[][] X;

int[][] G;
int zeit;
int[] dis, fin;
int[][] par;
long[][] ws;
int[] dep;

void dfs(int u, int p, long pw) {
  dis[u] = zeit++;
  par[0][u] = p;
  ws[0][u] = pw;
  dep[u] = (p == -1) ? 0 : (dep[p] + 1);
  foreach (i; G[u]) {
    const v = U[i] ^ V[i] ^ u;
    if (v != p) {
      dfs(v, u, W[i]);
    }
  }
  fin[u] = zeit++;
}

Tuple!(int, long) lca(int u, int v) {
  long ret;
  foreach_reverse (e; 0 .. E) {
    if (dep[u] - (1 << e) >= dep[v]) {
      ret += ws[e][u];
      u = par[e][u];
    }
    if (dep[v] - (1 << e) >= dep[u]) {
      ret += ws[e][v];
      v = par[e][v];
    }
  }
  foreach_reverse (e; 0 .. E) {
    if (par[e][u] != par[e][v]) {
      ret += ws[e][u];
      ret += ws[e][v];
      u = par[e][u];
      v = par[e][v];
    }
  }
  if (u != v) {
    ret += ws[0][u];
    ret += ws[0][v];
    u = par[0][u];
    v = par[0][v];
  }
  return tuple(u, ret);
}

void main() {
  try {
    for (; ; ) {
      N = readInt();
      U = new int[N - 1];
      V = new int[N - 1];
      W = new long[N - 1];
      foreach (i; 0 .. N - 1) {
        U[i] = readInt();
        V[i] = readInt();
        W[i] = readLong();
      }
      Q = readInt();
      K = new int[Q];
      X = new int[][Q];
      foreach (q; 0 .. Q) {
        K[q] = readInt();
        X[q] = new int[K[q]];
        foreach (k; 0 .. K[q]) {
          X[q][k] = readInt();
        }
      }
      
      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[U[i]] ~= i;
        G[V[i]] ~= i;
      }
      zeit = 0;
      dis = new int[N];
      fin = new int[N];
      par = new int[][](E, N);
      ws = new long[][](E, N);
      dep = new int[N];
      const rt = 0;
      dfs(rt, -1, 0);
      par[0][rt] = rt;
      foreach (e; 0 .. E - 1) {
        foreach (u; 0 .. N) {
          par[e + 1][u] = par[e][par[e][u]];
          ws[e + 1][u] = ws[e][u] + ws[e][par[e][u]];
        }
      }
      
      foreach (q; 0 .. Q) {
        X[q].sort!((a, b) => (dis[a] < dis[b]));
        auto us = X[q].dup;
        foreach (k; 0 .. K[q] - 1) {
          us ~= lca(X[q][k], X[q][k + 1])[0];
        }
        us.sort!((a, b) => (dis[a] < dis[b]));
        us = us.uniq.array;
        debug {
          writeln("us = ", us);
        }
        long ans;
        DList!int stack;
        foreach (u; us) {
          for (; !stack.empty && fin[stack.back] < dis[u]; ) {
            stack.removeBack;
          }
          if (!stack.empty) {
            ans += lca(stack.back, u)[1];
          }
          stack.insertBack(u);
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}
0