正文
增加 127 行删除 0 行
123456789101112131415161718192021# 题解:P17745 [GESP202609 六级] 分树规划# 思考## 题目大意## 思路历程# 解决## 正确思路## CODE::::success[AC Code]```cpp123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148# 题解:P17745 [GESP202609 六级] 分树规划提供一种思路较清晰但码量较长的大炮打蚊子考场做法。喜提 $10 \min$ 写完选择判断,接着 $30 \min$ 切本题,然后剩下时间不知道在干嘛(($\texttt{?} + \texttt{?} + 10 + 25$ 遗憾离场。报喜!报哀!可喜可贺!节哀顺变!# 思考## 题目大意一棵有 $n$ 个点的树,可将任意一条边删除,此时树分为两个连通块。使得两个联通块节点数的差值最小,求最小差值。## 思路历程似乎是典题板题?考场先尝试了 $\Omicron(n^3)$ 的做法,具体细节:- 暴力遍历每两个点之间的边;- 求出删除它之后两个联通块点数之差;- 求最小值,输出。拿到 $10 \textrm{ pts}$。注意到这是一颗树,一条边只可能连接着儿子与父亲,然后……# 解决## 正确思路记 $\operatorname{fa}_i$ 为 $i$ 号点的父节点。记 $\operatorname{cnt}_i$ 为“祖先集合包含 $i$ 号点”的节点数量,即从 $i$ 号点出发,不经过 $i$ 号点与 $\operatorname{fa}_i$ 号点的连边可到达的节点数量。如下图,$\operatorname{cnt}_4$ 为 $3$,即 $\{9, 10, 11\}$,$3$ 个点。~~很容易发现叶子节点的值为 1 然后递归上去就完事了,但我是区,喜欢大炮打蚊子。~~很容易注意到,如果将 $i$ 号点与 $\operatorname{fa}_i$ 号点的连边删除,那么“包含 $i$ 号点的连通块”节点数即为 $\operatorname{cnt}_i$,包含 $\operatorname{fa}_i$ 号点的连通块”节点数即为 $n - \operatorname{cnt}_i$。还有一个问题,怎么求 $\operatorname{cnt}$ 数组?等价于求不经过 $i$ 号点与 $\operatorname{fa}_i$ 号点的连边可到达的节点数量。注意到“树上两点间的简单路径仅有 $1$ 条”,因此 $i$ 号点与 $\operatorname{fa}_i$ 号点的连边仅有一条,我们不走它不就行了吗,最后算算遍历到了几个点就行了。理理思路:- 一遍 dfs 求出每个结点的父亲;- 对于 $i \in [2, n]$,每次一遍 dfs 按上述步骤求出 $\operatorname{cnt}_i$ ;- 统计 $\min^{i=2}_n |\operatorname{cnt}_i - (n - \operatorname{cnt}_i)|$,输出答案。时间复杂度 $\Omicron(n^2)$。## CODE并非考场代码(加了火车头),似乎喜提最劣最长解了。::::success[AC Code]```cpp#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 499122177using 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 !!!