跳到正文

题解

题解:P17745 [GESP202609 六级] 分树规划

CLC114514约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年9月13日
收录
2026年9月13日
更新
2026年9月13日
篇幅
约 6 分钟
获赞
1
题目
P17441

版本

  1. v2当前
  2. v1

题解:P17745 [GESP202609 六级] 分树规划

提供一种思路较清晰但码量较长的大炮打蚊子考场做法。

喜提 10min⁡10 \min 写完选择判断,接着 30min⁡30 \min 切本题,然后剩下时间不知道在干嘛((

?+?+10+25\texttt{?} + \texttt{?} + 10 + 25 遗憾离场。

报喜!报哀!可喜可贺!节哀顺变!

思考

题目大意

一棵有 nn 个点的树,可将任意一条边删除,此时树分为两个连通块。

使得两个联通块节点数的差值最小,求最小差值。

思路历程

似乎是典题板题?

考场先尝试了 O(n3)\Omicron(n^3) 的做法,具体细节:

  • 暴力遍历每两个点之间的边;
  • 求出删除它之后两个联通块点数之差;
  • 求最小值,输出。

拿到 10 pts10 \textrm{ pts}。

注意到这是一颗树,一条边只可能连接着儿子与父亲,然后……

解决

正确思路

记 fa⁡i\operatorname{fa}_i 为 ii 号点的父节点。

记 cnt⁡i\operatorname{cnt}_i 为“祖先集合包含 ii 号点”的节点数量,即从 ii 号点出发,不经过 ii 号点与 fa⁡i\operatorname{fa}_i 号点的连边可到达的节点数量。

如下图,cnt⁡4\operatorname{cnt}_4 为 33,即 {9,10,11}\{9, 10, 11\},33 个点。

很容易发现叶子节点的值为 1 然后递归上去就完事了,但我是区,喜欢大炮打蚊子。

很容易注意到,如果将 ii 号点与 fa⁡i\operatorname{fa}_i 号点的连边删除,那么“包含 ii 号点的连通块”节点数即为 cnt⁡i\operatorname{cnt}_i,包含 fa⁡i\operatorname{fa}_i 号点的连通块”节点数即为 n−cnt⁡in - \operatorname{cnt}_i。

还有一个问题,怎么求 cnt⁡\operatorname{cnt} 数组?等价于求不经过 ii 号点与 fa⁡i\operatorname{fa}_i 号点的连边可到达的节点数量。

注意到“树上两点间的简单路径仅有 11 条”,因此 ii 号点与 fa⁡i\operatorname{fa}_i 号点的连边仅有一条,我们不走它不就行了吗,最后算算遍历到了几个点就行了。

理理思路:

  • 一遍 dfs 求出每个结点的父亲;
  • 对于 i∈[2,n]i \in [2, n],每次一遍 dfs 按上述步骤求出 cnt⁡i\operatorname{cnt}_i ;
  • 统计 min⁡ni=2∣cnt⁡i−(n−cnt⁡i)∣\min^{i=2}_n |\operatorname{cnt}_i - (n - \operatorname{cnt}_i)|,输出答案。

时间复杂度 O(n2)\Omicron(n^2)。

CODE

并非考场代码(加了火车头),似乎喜提最劣最长解了。

AC Code
C++
#include<bits/stdc++.h>
//#include<windows.h>
//#include<mmsystem.h>
#define int long long
#define ULL unsigned long long
#define double long double
#define PII pair<int, int>
#define PQ priority_queue
#define LLinf 0x3f3f3f3f3f3f3f3f
#define eps 1e-9
//#define inverse_of_times_2_and_mod_998244353 499122177
using namespace std;
inline int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while(ch > 57 || ch < 48) {
        if(ch == 45) {
        	f = -1;
		}
        ch = getchar();
    }
    while(ch >= 48 && ch <= 57) {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}
vector<vector<int> > g(20005);
int fa[20005];
int cnt[20005];
bool vis[20005];
int n;
int u, v;
int ans = LLinf;
void dfs1(int x, int fat) {
	for(auto to : g[x]) {
		if(to != fat) {
			fa[to] = x;
			dfs1(to, x);
		}
	}
	return ;
}
void dfs2(int x, int fat) {
	vis[x] = true;
	for(auto to : g[x]) {
		if(to != fat && ! vis[to]) {
			dfs2(to, x);
		}
	}
	return ;
}
signed main(){
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	n = read();
	for(int i = 1; i < n; i ++) {
		u = read();
		v = read();
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs1(1, 0);
	for(int i = 2; i <= n; i ++) {
		memset(vis, false, sizeof(vis));
		dfs2(i, fa[i]);
		int cnt_ = 0;
		for(int j = 1; j <= n; j ++) {
			if(vis[j]) {
				cnt_ ++;
			}
		}
		cnt[i] = cnt_;
	}
	for(int i = 2; i <= n; i ++) {
		int t1 = cnt[i];
		int t2 = n - cnt[i];
		ans = min(ans, abs(t1 - t2));
	}
	cout << ans;
	return 0;
}
//TODO
//I AKed IOI !!!
原文