結果

問題 No.1002 Twotone
ユーザー keymoonkeymoon
提出日時 2024-02-23 03:45:13
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,443 ms / 5,000 ms
コード長 2,691 bytes
コンパイル時間 3,657 ms
コンパイル使用メモリ 274,204 KB
実行使用メモリ 72,088 KB
最終ジャッジ日時 2024-02-23 03:45:38
合計ジャッジ時間 22,013 ms
ジャッジサーバーID
(参考情報)
judge14 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 443 ms
37,224 KB
testcase_04 AC 561 ms
47,476 KB
testcase_05 AC 601 ms
47,480 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 309 ms
30,240 KB
testcase_08 AC 584 ms
47,488 KB
testcase_09 AC 532 ms
47,484 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 679 ms
37,932 KB
testcase_12 AC 894 ms
48,140 KB
testcase_13 AC 970 ms
48,140 KB
testcase_14 AC 2 ms
6,676 KB
testcase_15 AC 382 ms
28,988 KB
testcase_16 AC 803 ms
47,884 KB
testcase_17 AC 851 ms
47,884 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 627 ms
44,532 KB
testcase_20 AC 887 ms
54,212 KB
testcase_21 AC 968 ms
53,880 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 748 ms
41,136 KB
testcase_24 AC 1,443 ms
64,484 KB
testcase_25 AC 1,354 ms
64,544 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 121 ms
33,116 KB
testcase_28 AC 234 ms
47,680 KB
testcase_29 AC 191 ms
47,676 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 184 ms
47,560 KB
testcase_32 AC 265 ms
47,676 KB
testcase_33 AC 188 ms
47,676 KB
testcase_34 AC 996 ms
72,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct CentroidDecomposition {
  int n, r;
  vector<vector<int>> g, cd;
  vector<int> ord, szs;
  CentroidDecomposition(vector<vector<int>> &gg): g(gg), n(gg.size()), cd(n), szs(n,-1) {
    dfs1(0,-1);
    dfs(0,-1,n,-1);
  }
  int dfs1(int v, int p) {
    if(!szs[v]) return 0;
    szs[v]=1;
    for(auto e:g[v])if(e!=p)szs[v]+=dfs1(e,v);    
    return szs[v];
  }
  void dfs(int v, int p, int sz, int root){
    if (!szs[v]) return;
    begin:
    for(auto e:g[v]) {
      if (p==e or szs[e]<sz-sz/2) continue;
      p=v; v=e; goto begin;
    }
    if(root!=-1)cd[root].emplace_back(v);
    else r=v;
    ord.push_back(v);
    dfs1(v,-1);
    szs[v]=0;
    for(auto e:g[v])dfs(e,v,szs[e],v);
    return;
  }
};

int main() {
  cin.tie(0)->sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  using pii = pair<int, int>;
  vector<vector<int>> g(n);
  vector<vector<pii>> edges(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    u--, v--, c--;
    g[u].push_back(v);
    g[v].push_back(u);
    edges[u].emplace_back(v, c);
    edges[v].emplace_back(u, c);
  }
  CentroidDecomposition cd(g);
  vector<int> visited(n);
  ll ans = 0;
  for (int ce : cd.ord) {
    int cnt0 = 0; // *, -
    int cnt1 = 0; // *, *
    map<pii, int> cnt2; // x, y
    map<int, int> cnt3; // x, *
    map<int, int> cnt4; // x, -
    auto f1 = [&](auto self, int node, int parent, int c1, int c2) -> void {
      if (c2 == -1) {
        cnt0++;
        cnt4[c1]++;
      } else {
        cnt1++;
        cnt2[make_pair(min(c1, c2), max(c1, c2))]++;
        cnt3[c1]++;
        cnt3[c2]++;
      }
      for (auto [child, c] : edges[node]) {
        if (child == parent || visited[child]) continue;
        if (c1 == c) {
          self(self, child, node, c1, c2);
        } else if (c2 == c || c2 == -1) {
          self(self, child, node, c1, c);
        }
      }
    };
    auto f2 = [&](auto self, int node, int parent, int c1, int c2) -> void {
      if (c2 == -1) {
        ans += cnt0 - cnt4[c1] + cnt3[c1];
      } else {
        ans += cnt2[make_pair(min(c1, c2), max(c1, c2))] + cnt4[c1] + cnt4[c2];
      }
      for (auto [child, c] : edges[node]) {
        if (child == parent || visited[child]) continue;
        if (c1 == c) {
          self(self, child, node, c1, c2);
        } else if (c2 == c || c2 == -1) {
          self(self, child, node, c1, c);
        }
      }
    };
    for (auto [child, c] : edges[ce]) {
      if (visited[child]) continue;
      f2(f2, child, ce, c, -1);
      f1(f1, child, ce, c, -1);
    }
    ans += cnt1;
    visited[ce] = true;
  }
  cout << ans << endl;
}
0