제출 #1370564

#제출 시각아이디문제언어결과실행 시간메모리
1370564marcogambaroTree Decorations (CCO25_day1problem2)C++20
7 / 25
405 ms92544 KiB
#include <bits/stdc++.h>
#include <cassert>
using namespace std;
mt19937 timmy_loves_gambling(73);
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define readv(v) do { for(auto &i: v) cin >> i; } while(0)
#define _ << " " <<
#define lf "\n"
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define fi first
#define se second
template<typename... Args>
using vec = vector<Args...>;
#ifndef MARCO
bool dbg = 0;
#define infor(str, ...)
#define infof(str, ...)
#else
bool dbg = 1;
#define infor(str, ...) do { print(stderr, str, ##__VA_ARGS__); } while(0)
#define infof(str, ...) do { println(stderr, str, ##__VA_ARGS__); } while(0)
#endif

int N, M;
vec<vec<int>> G;

constexpr int K = 3;
constexpr int mod = 998244353;
vec<ll> P = {4358345, 3509843, 5498345};

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);   
    
	cin >> N >> M;
	G.resize(N);
	for(int i = 0; i < N-1; i++) {
		int a, b; cin >> a >> b;
		a--, b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}

	vec<array<ll, K>> st;
	vec<array<ll, K>> hash(N, {1, 3, 123589});
	vec<int> color(N), f(N), tried(N);
	vec<int> sz(N, 1), pt(N, 1);

	auto coloring = [&](int v, int from, auto &&self) -> void {
		for(auto &u: G[v]) {
			if(u == from) continue;
			self(u, v, self);
			sz[v] += sz[u];
			pt[v] += pt[u]+sz[u];

			for(int i=0; i<K; i++) {
				hash[v][i] = (hash[v][i] + hash[u][i]*P[i]%mod)%mod;
			}
		}

		st.push_back(hash[v]);
	};
	coloring(0, -1, coloring);

	sort(all(st));
	st.resize(unique(all(st)) - st.begin());
	for(int i = 0; i < N; i++) {
		color[i] = lower_bound(all(st), hash[i]) - st.begin();
		f[color[i]]++;
	}

	int ans = 0;
	bool ok;

	auto proc = [&](int v, int from, int d, int K, auto &&self) -> void {
		f[color[v]] += K*d;
		if(f[color[v]] < 0) ok = 0;

		for(auto &u: G[v]) {
			if(u == from) continue;
			self(u, v, d+1, K, self);
		}
	};

	auto dfs = [&](int v, int from, auto &&self) -> void {
		if(N-pt[v] == M && tried[color[v]] == 0) {
			ok = 1;
			proc(v, from, 1, -1, proc);
			if(ok) ans++;
			proc(v, from, 1, +1, proc);

			tried[color[v]] = 1;
		}

		for(auto &u: G[v]) {
			if(u == from) continue;
			self(u, v, self);
		}
	};
	dfs(0, -1, dfs);

	cout << ans << lf;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…