結果

問題 No.1002 Twotone
ユーザー 0w10w1
提出日時 2021-01-02 16:51:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3,779 ms / 5,000 ms
コード長 4,480 bytes
コンパイル時間 4,061 ms
コンパイル使用メモリ 244,172 KB
実行使用メモリ 111,772 KB
最終ジャッジ日時 2023-08-02 11:03:26
合計ジャッジ時間 44,804 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 7 ms
12,404 KB
testcase_02 AC 7 ms
11,620 KB
testcase_03 AC 847 ms
79,128 KB
testcase_04 AC 1,153 ms
89,828 KB
testcase_05 AC 1,201 ms
106,232 KB
testcase_06 AC 5 ms
9,448 KB
testcase_07 AC 846 ms
45,700 KB
testcase_08 AC 1,533 ms
79,000 KB
testcase_09 AC 1,525 ms
77,156 KB
testcase_10 AC 4 ms
5,204 KB
testcase_11 AC 1,852 ms
66,856 KB
testcase_12 AC 2,578 ms
73,740 KB
testcase_13 AC 2,503 ms
81,932 KB
testcase_14 AC 3 ms
5,824 KB
testcase_15 AC 1,280 ms
46,556 KB
testcase_16 AC 2,688 ms
75,948 KB
testcase_17 AC 2,614 ms
74,524 KB
testcase_18 AC 8 ms
14,444 KB
testcase_19 AC 1,779 ms
80,636 KB
testcase_20 AC 2,138 ms
88,040 KB
testcase_21 AC 2,091 ms
87,576 KB
testcase_22 AC 3 ms
5,884 KB
testcase_23 AC 1,978 ms
60,056 KB
testcase_24 AC 3,779 ms
95,236 KB
testcase_25 AC 3,676 ms
95,620 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 224 ms
63,740 KB
testcase_28 AC 460 ms
81,960 KB
testcase_29 AC 361 ms
86,148 KB
testcase_30 AC 3 ms
4,824 KB
testcase_31 AC 354 ms
85,248 KB
testcase_32 AC 517 ms
81,400 KB
testcase_33 AC 385 ms
88,824 KB
testcase_34 AC 1,014 ms
111,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

/*^ vector extensions  */

template<typename T>
vector<T> concat(vector<T> a, vector<T> b) {
  a.insert(a.end(), b.begin(), b.end());
  return a;
}

template<typename T, size_t sz>
struct _Matrix_type { typedef vector<typename _Matrix_type<T, sz - 1>::type> type; };
template<typename T>
struct _Matrix_type<T, 1> { typedef T type; };
template<typename T>
struct _Matrix {
  static auto build(size_t s) { return vector<T>(s); }
  template<typename ...Args>
  static auto build(size_t f, Args... args) {
    return vector<typename _Matrix_type<T, 1 + sizeof...(args)>::type>(f, _Matrix<T>::build(args...));
  }
};
template<typename T, typename... Args>
auto buildMatrix(Args... args) { return _Matrix<T>::build(args...); }

/*  vector extensions $*/

/*^ generic definitions  */

template<typename F>
struct _RecurFun : F {
  _RecurFun(F&& f) : F(forward<F>(f)) {}
  template<typename... Args>
  decltype(auto) operator()(Args&&... args) const { return F::operator()(*this, forward<Args>(args)...); }
};

template<typename F>
decltype(auto) RecurFun(F&& f) { return _RecurFun<F> { forward<F>(f) }; }

/*  generic definitions $*/

/*^ pbds  */

#if __has_include(<ext/pb_ds/assoc_container.hpp>)
  #include <ext/pb_ds/assoc_container.hpp>
  using namespace __gnu_pbds;

  const int RandomIntSeed = chrono::high_resolution_clock::now().time_since_epoch().count();
  struct HashInt { int operator()(int x) const { return x ^ RandomIntSeed; } };

  template<typename T, typename U, typename H = hash<T>>
  using Gmap = gp_hash_table<T, U, H>;
#endif

/*  pbds $*/

struct Centroid {
  vector<vector<int>> graph;
  vector<int> size, dead;
  Centroid(const vector<vector<int>> &g) : size(g.size()), dead(g.size()), graph(g) {}
  vector<int> findCentroids(int root) {
    vector<int> centroids;
    RecurFun([&](auto buildSize, int u, int p) -> void {
      size[u] = 1;
      for (int v : graph[u]) if (v != p && !dead[v]) buildSize(v, u), size[u] += size[v];
    })(root, -1);
    RecurFun([&](auto buildCentroids, int u, int p) -> void {
      int ok = (size[root] - size[u]) * 2 <= size[root];
      for (int v : graph[u]) if (v != p && !dead[v]) buildCentroids(v, u), ok &= size[v] * 2 <= size[root];
      if (ok) centroids.push_back(u);
    })(root, -1);
    assert(1 <= centroids.size() && centroids.size() <= 2);
    return centroids;
  }
};

int main() {
  ios::sync_with_stdio(false);

  int N, K; {
    cin >> N >> K;
  }

  vector<vector<int>> G(N); 
  vector<unordered_map<int, int>> C(N); {
    for (int u = 0; u + 1 < N; ++u) {
      int U, V, Z; { 
        cin >> U >> V >> Z;
        --U, --V, --Z;
      }
      G[U].push_back(V);
      G[V].push_back(U);
      C[U][V] = Z;
      C[V][U] = Z;
    }
  }

  int64_t res = 0; {
    Centroid cd(G);

    queue<int> que({ cd.findCentroids(0)[0] });
    int all = 0; // single color
    vector<int> cnt(K), cntx(K);
    vector<unordered_map<int, int>> cnt2(K);
    while (que.size()) {
      int u = que.front();
      que.pop();

      for (int v : G[u]) if (!cd.dead[v]) {
        RecurFun([&](auto count, int u, int p, int a, int b) -> void {
          for (int v : G[u]) if (v != p && !cd.dead[v]) {
            if (a == C[u][v] || b == C[u][v]) count(v, u, a, b);
            else if (a == -1) count(v, u, C[u][v], b);
          }
          if (a == -1) res += (all - cnt[b]) + cntx[b];
          else res += 1 + cnt[a] + cnt[b] + cnt2[a][b];
        })(v, u, -1, C[u][v]);

        RecurFun([&](auto update, int u, int p, int a, int b) -> void {
          for (int v : G[u]) if (v != p && !cd.dead[v]) {
            if (a == C[u][v] || b == C[u][v]) update(v, u, a, b);
            else if (a == -1) update(v, u, C[u][v], b);
          }
          if (a == -1) ++cnt[b], ++all;
          else ++cntx[a], ++cntx[b], ++cnt2[a][b], ++cnt2[b][a];
        })(v, u, -1, C[u][v]);
      }

      for (int v : G[u]) if (!cd.dead[v]) {
        RecurFun([&](auto update, int u, int p, int a, int b) -> void {
          for (int v : G[u]) if (v != p && !cd.dead[v]) {
            if (a == C[u][v] || b == C[u][v]) update(v, u, a, b);
            else if (a == -1) update(v, u, C[u][v], b);
          }
          if (a == -1) --cnt[b], --all;
          else --cntx[a], --cntx[b], --cnt2[a][b], --cnt2[b][a];
        })(v, u, -1, C[u][v]);
      }

      cd.dead[u] = 1;
      for (int v : G[u]) if (!cd.dead[v]) que.push(cd.findCentroids(v)[0]);
    }
  }

  cout << res << endl;
}

0