제출 #1370567

#제출 시각아이디문제언어결과실행 시간메모리
1370567marcogambaroTree Decorations (CCO25_day1problem2)C++20
17 / 25
536 ms178524 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;

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);
	}

	map<vec<int>, int> mp;
	int id = 1;

	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 {
		vec<int> hsh;

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

			hsh.push_back(color[u]);
		}

		sort(all(hsh));
		if(mp[hsh] == 0) mp[hsh] = id++;
		color[v] = mp[hsh];
		f[color[v]]++;
	};
	coloring(0, -1, coloring);

	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;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…