結果

問題 No.1002 Twotone
ユーザー keymoonkeymoon
提出日時 2024-02-23 02:22:58
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,368 bytes
コンパイル時間 6,476 ms
コンパイル使用メモリ 298,740 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-02-23 02:23:13
合計ジャッジ時間 12,825 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef LOCAL
#pragma GCC optimize("O3,no-stack-protector")
#pragma GCC target("arch=x86-64-v4")
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

namespace std {
template<typename T,typename U> struct hash<pair<T,U>> {
size_t operator()(pair<T,U> const &val) const {
return hash<T>{}(val.first)*1337^hash<U>{}(val.second);
}};}

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

bool DEBUG = false;

using pii = pair<int, int>;

int cnt0 = 0; // *, -
int cnt1 = 0; // *, *
map<pii, int> cnt2; // x, y
map<int, int> cnt3; // x, *
map<int, int> cnt4; // x, -

vector<vector<int>> g;
vector<vector<pii>> edges;
vector<int> visited;
void f1(int node, int parent, int c1, int c2) {
  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) {
      f1(child, node, c1, c2);
    } else if (c2 == c || c2 == -1) {
      f1(child, node, c1, c);
    }
  }
};
ll ans;
void f2(int node, int parent, int c1, int c2) {
  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) {
      f2(child, node, c1, c2);
    } else if (c2 == c || c2 == -1) {
      f2(child, node, c1, c);
    }
  }
};

int main() {
  auto now = [](){ return chrono::system_clock::now(); };
  auto start = now();
  auto cur = [&](){ return (now()-start).count() / 1000'000.; };
  cin.tie(0)->sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  g = vector<vector<int>>(n);
  edges = vector<vector<pii>>(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);
  }
  if (DEBUG) cout << cur() << endl;
  CentroidDecomposition cd(g);
  for (int i = 1; i < 0; i++) {
    cd = CentroidDecomposition(g);
  }
  if (DEBUG) cout << cur() << endl;
  visited = vector<int>(n);
  ans = 0;

  for (int ce : cd.ord) {
    cnt0=cnt1=0;
    cnt2.clear();
    cnt3.clear();
    cnt4.clear();

    for (auto [child, c] : edges[ce]) {
      if (visited[child]) continue;
      f2(child, ce, c, -1);
      f1(child, ce, c, -1);
    }
    ans += cnt1;
    visited[ce] = true;
  }

  if (DEBUG) cout << cur() << endl;
  cout << ans << endl;
}
0