結果

問題 No.1002 Twotone
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-02-28 22:05:32
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 2,828 ms / 5,000 ms
コード長 4,355 bytes
コンパイル時間 967 ms
コンパイル使用メモリ 131,764 KB
実行使用メモリ 66,872 KB
最終ジャッジ日時 2024-06-22 05:37:35
合計ジャッジ時間 37,751 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 614 ms
20,740 KB
testcase_04 AC 831 ms
18,464 KB
testcase_05 AC 866 ms
19,064 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 774 ms
14,760 KB
testcase_08 AC 1,409 ms
32,020 KB
testcase_09 AC 1,543 ms
18,628 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 1,612 ms
17,384 KB
testcase_12 AC 2,070 ms
32,316 KB
testcase_13 AC 2,195 ms
22,336 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 1,135 ms
14,508 KB
testcase_16 AC 2,311 ms
18,888 KB
testcase_17 AC 2,241 ms
22,124 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 1,239 ms
35,812 KB
testcase_20 AC 1,467 ms
52,192 KB
testcase_21 AC 1,468 ms
50,732 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 1,530 ms
35,344 KB
testcase_24 AC 2,828 ms
66,872 KB
testcase_25 AC 2,687 ms
65,648 KB
testcase_26 AC 1 ms
6,944 KB
testcase_27 AC 212 ms
16,320 KB
testcase_28 AC 345 ms
26,652 KB
testcase_29 AC 322 ms
22,992 KB
testcase_30 AC 2 ms
6,944 KB
testcase_31 AC 312 ms
19,964 KB
testcase_32 AC 341 ms
22,524 KB
testcase_33 AC 324 ms
23,244 KB
testcase_34 AC 2,348 ms
57,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


int N, K;
int[] A, B, C;
int[][] G;
long ans;

void solveCentroidDecomp() {
  auto sz = new int[N];
  auto del = new bool[N];
  void dfsSz(int u, int p) {
    sz[u] = 1;
    foreach (i; G[u]) {
      const v = A[i] ^ B[i] ^ u;
      if (v != p) {
        dfsSz(v, u);
        sz[u] += sz[v];
      }
    }
  }
  dfsSz(0, -1);

  // r: centroid
  void solveSubtree(int r) {
    debug {
      writefln("solveSubtree r = %s (del = %s)", r, del);
    }
    
    long[long] freq1Sum, freq2Sum;
    long[long] freq1, freq2;
    
    void dfs(int u, int p, int[] cs) {
      if (cs.length == 1) {
        ++freq1Sum[cs[0]];
        ++freq1[cs[0]];
      } else {
        const key = cs[0] + cs[1] * cast(long)(K);
        ++freq2Sum[key];
        ++freq2[key];
      }
      foreach (i; G[u]) {
        const v = A[i] ^ B[i] ^ u;
        if (!del[v] && v != p) {
          auto ccs = cs ~ C[i];
          ccs = ccs.sort.uniq.array;
          if (ccs.length <= 2) {
            dfs(v, u, ccs);
          }
        }
      }
    }
    
    foreach (i; G[r]) {
      const v = A[i] ^ B[i] ^ r;
      if (!del[v]) {
        freq1.clear;
        freq2.clear;
        dfs(v, r, [C[i]]);
        
        const num1 = freq1.values.sum;
        foreach (c, val; freq1) {
          // 1-1
          ans -= val * (num1 - val);
        }
        // 2-1, 2-2
        foreach (key, val; freq2) {
          const c0 = key % K;
          const c1 = key / K;
          // 2-1
          ans -= 2 * val * (((c0 in freq1) ? freq1[c0] : 0) + ((c1 in freq1) ? freq1[c1] : 0));
          // 2-2
          ans -= val * val;
        }
      }
    }
    
    const num1Sum = freq1Sum.values.sum;
    foreach (c, val; freq1Sum) {
      // 1-1
      ans += val * (num1Sum - val);
    }
    // 2-1, 2-2
    foreach (key, val; freq2Sum) {
      const c0 = key % K;
      const c1 = key / K;
      // 2-0
      ans += 2 * val;
      // 2-1
      ans += 2 * val * (((c0 in freq1Sum) ? freq1Sum[c0] : 0) + ((c1 in freq1Sum) ? freq1Sum[c1] : 0));
      // 2-2
      ans += val * val;
    }
  }

  void solveRec(int u) {
    for (; ; ) {
      int vm = -1;
      foreach (i; G[u]) {
        const v = A[i] ^ B[i] ^ u;
        if (!del[v]) {
          if (vm == -1 || sz[vm] < sz[v]) {
            vm = v;
          }
        }
      }
      if (vm == -1 || sz[u] >= 2 * sz[vm]) {
        solveSubtree(u);
        del[u] = true;
        foreach (i; G[u]) {
          const v = A[i] ^ B[i] ^ u;
          if (!del[v]) {
            solveRec(v);
          }
        }
        break;
      } else {
        sz[u] -= sz[vm];
        sz[vm] += sz[u];
        u = vm;
      }
    }
  }
  solveRec(0);
}



void main() {
  try {
    for (; ; ) {
      N = readInt();
      K = readInt();
      A = new int[N - 1];
      B = new int[N - 1];
      C = new int[N - 1];
      foreach (i; 0 .. N - 1) {
        A[i] = readInt() - 1;
        B[i] = readInt() - 1;
        C[i] = readInt() - 1;
      }
      
      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }
      ans = 0;
      solveCentroidDecomp();
      ans /= 2;
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0