結果

問題 No.1002 Twotone
ユーザー Yu_212Yu_212
提出日時 2024-02-22 23:34:58
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,608 bytes
コンパイル時間 3,955 ms
コンパイル使用メモリ 275,872 KB
実行使用メモリ 69,828 KB
最終ジャッジ日時 2024-02-22 23:35:24
合計ジャッジ時間 24,350 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 825 ms
69,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct CentroidDecomposition {
  vector<vector<int>> g, cd;
  vector<int> used, ord;
  int n, r;
  CentroidDecomposition(vector<vector<int>> &g_): g(g_) {
    n = g_.size();
    used = vector<int>(n);
    cd = vector<vector<int>>(n);
    dfs(0,-1,n,-1);
  }
  int dfs(int node, int p, int sz, int root){
    if(used[node])return 0;
    bool b=true;
    int res=1;
    for(auto e:g[node]){
      if(p==e)continue;
      int t=dfs(e,node,sz,root);
      res+=t;
      if(t>sz/2)b=false;
    }
    if(!b||sz-res>sz/2)return res;
    if(root!=-1)cd[root].emplace_back(node);
    else r=node;
    ord.push_back(node);
    used[node]=1;
    for(auto e:g[node])dfs(e,node,dfs(e,node,n*2,node),node);
    return n*2;
  }
};

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, *
    auto f1 = [&](auto self, int node, int parent, int c1, int c2) -> void {
      if (c2 == -1) {
        cnt0++;
      } 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 + cnt3[c1];
      } else {
        ans += cnt2[make_pair(min(c1, c2), max(c1, 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