【CF1042F】Leaf Sets

【CF1042F】Leaf Sets

题面

洛谷

题解

对于一个根节点(x),考虑其子树内的所有(lca)为它的叶子节点到它的距离(d_1<d2<...<d_m)

那么对于中间最小的(d_i+d_{i+1}>K),我们可以将(i)之前的所有叶子节点合并成一个大点,并以深度(d_i)向上合并,再将(d_{i+1}...d_m)向上合并即可。

这样子用数据结构维护复杂度是(O(nlog n))的。

然而我们发现只有(d_i)继续向上合并才有用,那么只要保留这个(d_i)就行了,复杂度(O(n))

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring> 
#include <cmath> 
#include <algorithm>
using namespace std; 
inline int gi() {
    register int data = 0, w = 1;
    register char ch = 0;
    while (!isdigit(ch) && ch != '-') ch = getchar(); 
    if (ch == '-') w = -1, ch = getchar(); 
    while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar(); 
    return w * data; 
} 
const int MAX_N = 1e6 + 5; 
struct Graph { int to, next; } e[MAX_N << 1]; 
int fir[MAX_N], e_cnt; 
void clearGraph() { memset(fir, -1, sizeof(fir)); e_cnt = 0; } 
void Add_Edge(int u, int v) { e[e_cnt] = (Graph){v, fir[u]}, fir[u] = e_cnt++; } 
int N, K, deg[MAX_N], ans; 
int dfs(int x, int fa) { 
	if (deg[x] == 1) return 1; 
	int res = 0; 
	for (int i = fir[x]; ~i; i = e[i].next) { 
		int v = e[i].to; if (v == fa) continue;
		int tmp = dfs(v, x); 
		if (tmp + res > K) ans++, res = min(res, tmp); 
		else res = max(res, tmp); 
	} 
	return res ? res + 1 : 0; 
} 
int main () { 
#ifndef ONLINE_JUDGE 
    freopen("cpp.in", "r", stdin); 
#endif 
	clearGraph(); 
	N = gi(), K = gi(); 
	for (int i = 1; i < N; i++) { 
		int u = gi(), v = gi(); 
		Add_Edge(u, v), Add_Edge(v, u); 
		deg[u]++, deg[v]++; 
	} 
	for (int i = 1; i <= N; i++)
		if (deg[i] != 1) return printf("%d
", (bool)(dfs(i, 0)) + ans) & 0; 
    return 0; 
} 
原文地址:https://www.cnblogs.com/heyujun/p/11678512.html