結果

問題 No.901 K-ary εxtrεεmε
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-10-04 23:37:33
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 299 ms / 3,000 ms
コード長 3,390 bytes
コンパイル時間 779 ms
コンパイル使用メモリ 113,988 KB
実行使用メモリ 40,680 KB
最終ジャッジ日時 2023-09-04 02:57:09
合計ジャッジ時間 8,895 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 171 ms
40,568 KB
testcase_01 AC 2 ms
4,500 KB
testcase_02 AC 4 ms
4,376 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 231 ms
31,940 KB
testcase_08 AC 231 ms
32,480 KB
testcase_09 AC 232 ms
31,948 KB
testcase_10 AC 232 ms
31,964 KB
testcase_11 AC 227 ms
32,420 KB
testcase_12 AC 244 ms
35,328 KB
testcase_13 AC 239 ms
33,608 KB
testcase_14 AC 240 ms
33,772 KB
testcase_15 AC 244 ms
34,360 KB
testcase_16 AC 240 ms
32,500 KB
testcase_17 AC 260 ms
37,596 KB
testcase_18 AC 260 ms
36,840 KB
testcase_19 AC 259 ms
37,580 KB
testcase_20 AC 257 ms
35,776 KB
testcase_21 AC 260 ms
36,320 KB
testcase_22 AC 264 ms
35,448 KB
testcase_23 AC 258 ms
35,052 KB
testcase_24 AC 259 ms
33,796 KB
testcase_25 AC 255 ms
33,940 KB
testcase_26 AC 255 ms
33,476 KB
testcase_27 AC 292 ms
39,252 KB
testcase_28 AC 297 ms
38,964 KB
testcase_29 AC 299 ms
40,680 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 = 17;

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 + 1, N);
      ws = new long[][](E + 1, N);
      dep = new int[N];
      const rt = 0;
      dfs(rt, -1, 0);
      par[0][rt] = rt;
      foreach (e; 0 .. E) {
        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]));
        long ans;
        foreach (k; 0 .. K[q]) {
          ans += lca(X[q][k], X[q][(k + 1) % K[q]])[1];
        }
        ans /= 2;
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}
0