[CF1039D]You Are Given a Tree

题目大意:给你一棵$n(n\leqslant10^5)$个点的树,一个简单路径的集合$S_k$被称为$k$合法当且仅当:树的每个节点至多属于其中一条路径,且每条路径恰好包含$k$个点。对于$k\in[1,n]$,$|S_k|$的最大值

题解:树形$DP$,$O(n^2)$的很好想,记录一个节点向下最长的没有被选的链长度和以这个点为根的子树的答案,转移显然。

发现对于答案$ans_k$,$ans_k\times k\leqslant n$,当$k\leqslant \sqrt n$时,$ans_k$最多只有$\sqrt n$个;当$k\geqslant\sqrt n$时,$ans_k\leqslant\sqrt n$,取值范围只有$\sqrt n$个,于是$ans_k$个数最多$2\sqrt n$个,可以二分相同的区间。复杂度$O(n\sqrt n\log_2n)$,但是这样会$TLE$。

猜想是$dfs$常数过大,于是按$dfs$序逆序排列后递推,就过了

卡点:$TLE$

 

C++ Code:

#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 100010
int head[maxn], cnt;
struct Edge {
	int to, nxt;
} e[maxn << 1];
inline void add(int a, int b) {
	e[++cnt] = (Edge) {b, head[a]}; head[a] = cnt;
}

int n;
int ans[maxn];

int f[maxn], rem[maxn], fa[maxn];
int dfn[maxn], idx, rnk[maxn];
void dfs(int u) {
	dfn[u] = ++idx; rnk[u] = u;
	for (int i = head[u]; i; i = e[i].nxt) {
		int v = e[i].to;
		if (v != fa[u]) {
			fa[v] = u;
			dfs(v);
		}
	}
}
inline bool cmp(int a, int b) {return dfn[a] > dfn[b];}

int max_1[maxn], max_2[maxn];
inline void work(const int k) {
	memset(f, 0, sizeof f);
	memset(rem, 0, sizeof rem);
	memset(max_1, 0, sizeof max_1);
	memset(max_2, 0, sizeof max_2);
	for (int I = 1, u, fa; I <= n; I++) {
		u = rnk[I];
		if (max_1[u] + max_2[u] + 1 >= k) {
			f[u]++;
			rem[u] = 0;
		} else rem[u] = max_1[u] + 1;
		fa = ::fa[u];
		f[fa] += f[u];
		if (max_1[fa] < rem[u]) {
			max_2[fa] = max_1[fa];
			max_1[fa] = rem[u];
		} else if (max_2[fa] < rem[u]) max_2[fa] = rem[u];
	}
	ans[k] = f[1];
}

int main() {
	scanf("%d", &n);
	for (int i = 1, a, b; i < n; i++) {
		scanf("%d%d", &a, &b);
		add(a, b);
		add(b, a);
	}
	memset(ans, -1, sizeof ans);
	dfs(1);
	std::sort(rnk + 1, rnk + n + 1, cmp);
	ans[1] = n;
	int l, r;
	for (l = 2; l <= n; l = r + 1) {
		if (ans[l] == -1) work(l);
		int last = ans[l];
		int res = l, L = l, R = n;
		while (L <= R) {
			int mid = L + R >> 1;
			if (ans[mid] == -1) work(mid);
			if (ans[mid] == last) {
				res = mid;
				L = mid + 1;
			} else R = mid - 1;
		}
		r = res;
		for (int i = l; i <= r; i++) ans[i] = last;
	}
	for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
	return 0;
}

  

转载于:https://www.cnblogs.com/Memory-of-winter/p/9866045.html

# COT - Count on a tree ## 题面翻译 # 本题必须使用 C++98 提交 给你一棵有n个结点的树,节点编号为1~n。 每个节点都有一个权值。 要求执行以下操作: U V K:求从节点u到节点v的第k小权值。 # 输入输出格式 ## 输入格式 第一行有两个整数n和m(n,m≤100000) 第二行有n个整数。 第i个整数表示第i个节点的权值。 接下来的n-1行中,每行包含两个整数u v,表示u和v之间有一条边。 接下来的m行,每行包含三个整数U V K,进行一次操作。 ## 输出格式 对于每个操作,输出结果。 ## 题目描述 You are given a tree with **N** nodes. The tree nodes are numbered from **1** to **N**. Each node has an integer weight. We will ask you to perform the following operation: - **u v k** : ask for the kth minimum weight on the path from node **u** to node **v** ## 输入格式 In the first line there are two integers **N** and **M**. (**N, M** <= 100000) In the second line there are **N** integers. The ith integer denotes the weight of the ith node. In the next **N-1** lines, each line contains two integers **u** **v**, which describes an edge (**u**, **v**). In the next **M** lines, each line contains three integers **u** **v** **k**, which means an operation asking for the kth minimum weight on the path from node **u** to node **v**. ## 输出格式 For each operation, print its result. ## 样例 #1 ### 样例输入 #1 ``` 8 5 105 2 9 3 8 5 7 7 1 2 1 3 1 4 3 5 3 6 3 7 4 8 2 5 1 2 5 2 2 5 3 2 5 4 7 8 2 ``` ### 样例输出 #1 ``` 2 8 9 105 7 ```
最新发布
07-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值