Submission #1362990

#TimeUsernameProblemLanguageResultExecution timeMemory
1362990tolbiTree Decorations (CCO25_day1problem2)C++20
25 / 25
495 ms199976 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int H = 3;
typedef array<int,H> Hash;
constexpr Hash MOD = {1000000007, 998244353, 998244853};
int32_t main(){
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int N, M;
  cin >> N >> M;
  vector<Hash> fp(N * 2 + 5);
  fp[0] = {1, 1, 1};
  for (int i = 1; i < fp.size(); ++i) {
    for (int j = 0; j < H; ++j) {
      fp[i][j] = fp[i-1][j] * 2;
      if (fp[i][j] >= MOD[j]) fp[i][j] -= MOD[j];
    }
  }
  vector<vector<int>> tree(N);
  for (int i = 0; i < N - 1; ++i) {
    int u, v;
    cin >> u >> v;
    tree[u-1].push_back(v-1);
    tree[v-1].push_back(u-1);
  }
  vector<Hash> hash(N);
  vector<int> red(N), subsz(N, 1), par(N, -1);
  auto dfs = [&](int node, int lnode, auto&&dfs)->void{
    par[node] = lnode;
    vector<pair<Hash, int>> c;
    for (auto it : tree[node]) if (it != lnode) {
      dfs(it, node, dfs);
      c.push_back({hash[it], subsz[it]});
      subsz[node] += subsz[it];
      red[node] += red[it];
    }
    red[node] += subsz[node];
    sort(c.begin(), c.end());
    c.push_back({{1, 1, 1}, 1});
    int pref = 1;
    for (auto &[hsh, sz] : c) {
      for (int i = 0; i < H; ++i) {
        hash[node][i] += (hsh[i] * fp[pref][i])%MOD[i];
        if (hash[node][i] >= MOD[i]) hash[node][i] -= MOD[i];
      }
      pref += sz * 2;
    }
  };
  dfs(0, -1, dfs);
  map<Hash, int> mp;
  for (int i = 0; i < N; ++i) {
    mp[hash[i]]++;
  }
  set<Hash> ans;
  for (int nd = 0; nd < N; ++nd) {
    if (red[nd] + M == N) {
      map<Hash, int> t;
      queue<array<int,3>> q;
      q.push({nd, par[nd], 1});
      while (q.size()) {
        int node = q.front()[0];
        int lnode = q.front()[1];
        int w = q.front()[2];
        q.pop();
        t[hash[node]] += w;
        for (auto it : tree[node]) if (it != lnode) {
          q.push({it, node, w + 1});
        }
      }
      for (auto [h, k] : t) {
        if (mp[h] < k) {
          goto mahmut;
        }
      }
      ans.insert(hash[nd]);
      mahmut:;
    }
  }
  cout << ans.size() << '\n';
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...