結果

問題 No.898 tri-βutree
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-10-04 22:21:00
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 750 ms / 4,000 ms
コード長 3,796 bytes
コンパイル時間 1,357 ms
コンパイル使用メモリ 136,628 KB
実行使用メモリ 59,948 KB
最終ジャッジ日時 2024-06-22 02:41:08
合計ジャッジ時間 15,283 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

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] = 3;
        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