結果

問題 No.1002 Twotone
ユーザー 👑 emthrmemthrm
提出日時 2020-03-01 04:37:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,349 bytes
コンパイル時間 3,879 ms
コンパイル使用メモリ 241,580 KB
実行使用メモリ 59,648 KB
最終ジャッジ日時 2024-04-21 22:04:12
合計ジャッジ時間 15,588 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 308 ms
29,952 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 2 ms
5,376 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 2 ms
5,376 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 2 ms
5,376 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 106 ms
26,364 KB
testcase_28 AC 195 ms
37,940 KB
testcase_29 AC 159 ms
37,800 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 155 ms
37,792 KB
testcase_32 AC 204 ms
37,812 KB
testcase_33 AC 163 ms
37,944 KB
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

using CostType = int;
struct Edge {
  int src, dst; CostType cost;
  Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
  inline bool operator<(const Edge &rhs) const {
    return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src;
  }
  inline bool operator<=(const Edge &rhs) const { return !(rhs < *this); }
  inline bool operator>(const Edge &rhs) const { return rhs < *this; }
  inline bool operator>=(const Edge &rhs) const { return !(*this < rhs); }
};

struct CentroidDecomposition {
  int root;
  vector<vector<int> > comp;
  vector<int> par;

  CentroidDecomposition(const vector<vector<Edge> > &graph) : graph(graph) {
    int n = graph.size();
    alive.assign(n, true);
    subtree.resize(n);
    comp.resize(n);
    par.assign(n, -1);
    root = build(0);
  }

private:
  const vector<vector<Edge> > graph;
  vector<bool> alive;
  vector<int> subtree;

  int build(int s) {
    int centroid = search_centroid(-1, s, calc_subtree(-1, s) / 2);
    alive[centroid] = false;
    for (const Edge &e : graph[centroid]) {
      if (alive[e.dst]) {
        comp[centroid].emplace_back(build(e.dst));
        par[e.dst] = centroid;
      }
    }
    alive[centroid] = true;
    return centroid;
  }

  int calc_subtree(int par, int ver) {
    subtree[ver] = 1;
    for (const Edge &e : graph[ver]) {
      if (e.dst != par && alive[e.dst]) subtree[ver] += calc_subtree(ver, e.dst);
    }
    return subtree[ver];
  }

  int search_centroid(int par, int ver, int half) {
    for (const Edge &e : graph[ver]) {
      if (e.dst != par && alive[e.dst]) {
        if (subtree[e.dst] > half) return search_centroid(ver, e.dst, half);
      }
    }
    return ver;
  }
};

int main() {
  int n, k; cin >> n >> k;
  vector<vector<Edge> > graph(n);
  REP(_, n - 1) {
    int u, v, c; cin >> u >> v >> c; --u; --v; --c;
    graph[u].emplace_back(u, v, c);
    graph[v].emplace_back(v, u, c);
  }
  CentroidDecomposition cd(graph);
  vector<bool> visited(n, false);
  ll ans = 0;
  function<void(int)> dfs = [&](int ver) {
    visited[ver] = true;
    map<int, ll> mp1;
    ll one = 0;
    map<pair<int, int>, ll> mp2;
    for (const Edge &e : graph[ver]) {
      if (visited[e.dst]) continue;
      map<int, int> nx_mp1;
      map<pair<int, int>, int> nx_mp2;
      map<int, int> now;
      now[e.cost] = 1;
      ans += one - (mp1.count(e.cost) == 1 ? mp1[e.cost] : 0);
      nx_mp1[e.cost] = 1;
      function<void(int, int)> dfs2 = [&](int p, int v) {
        for (const Edge &d : graph[v]) {
          if (d.dst == p || visited[d.dst]) continue;
          ++now[d.cost];
          if (now.size() == 1) {
            int x = now.begin()->first;
            ans += one - (mp1.count(x) == 1 ? mp1[x] : 0);
            ++nx_mp1[x];
          } else if (now.size() == 2) {
            int x = now.begin()->first, y = next(now.begin())->first;
            ans += (mp1.count(x) == 1 ? mp1[x] : 0) + (mp1.count(y) == 1 ? mp1[y] : 0) + (mp2.count({x, y}) == 1 ? mp2[{x, y}] : 0) + 1;
            ++nx_mp2[{x, y}];
          }
          if (now.size() <= 2) dfs2(v, d.dst);
          --now[d.cost];
          if (now[d.cost] == 0) now.erase(d.cost);
        }
      };
      dfs2(ver, e.dst);
      for (auto pr : nx_mp1) {
        mp1[pr.first] += pr.second;
        one += pr.second;
      }
      for (auto pr : nx_mp2) mp2[pr.first] += pr.second;
    }
    for (int e : cd.comp[ver]) dfs(e);
  };
  dfs(cd.root);
  cout << ans << '\n';
  return 0;
}
0