main
黄海 2 years ago
parent a057b134e0
commit c9db075ab8

@ -0,0 +1,50 @@
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
typedef long long LL;
vector<LL> g[maxn];
LL siz[maxn], son[maxn];
LL r1, r2, n;
void dfs(LL u, LL fa) {
siz[u] = 1;
son[u] = 0;
for (LL i = 0; i < g[u].size(); i++) {
LL v = g[u][i];
if (v == fa) continue;
dfs(v, u);
siz[u] += siz[v];
son[u] = max(son[u], siz[v]);
}
son[u] = max(son[u], n - siz[u]);
if ((son[u] << 1) <= n) r2 = r1, r1 = u;
}
int main() {
LL t;
cin >> t;
while (t--) {
cin >> n;
for (LL i = 0; i <= n + 10; i++) g[i].clear(), siz[i] = 0, son[i] = 0;
for (LL i = 1; i < n; i++) {
LL x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
r1 = r2 = 0;
dfs(1, 0);
if (!r2) {
LL r3 = g[r1][0];
cout << r1 << " " << r3 << endl;
cout << r1 << " " << r3 << endl;
} else {
LL r3 = r1;
for (LL i = 0; i < g[r2].size(); i++) {
r3 = g[r2][i];
if (r3 != r1) break;
}
cout << r3 << " " << r2 << endl;
cout << r3 << " " << r1 << endl;
}
}
return 0;
}

@ -11,7 +11,7 @@
### 题单
#### **[$POI2008$ $STA-Station$](https://www.luogu.com.cn/problem/P3478)**
#### **[$P3478$ $STA-Station$](https://www.luogu.com.cn/problem/P3478)**
> **题意**:给定一个$n$个点的无根树,问以树上哪个节点为根时,其所有节点的深度和最大?
**深度**:节点到根的简单路径上边的数量
@ -114,8 +114,110 @@ signed main() {
- 第一次搜索完成预处理(如子树大小等),同时得到该节点的解。
- 第二次搜索进行换根的动态规划,由已知解的节点推出相连节点的解。
#### [$C$. $Link$ $Cut$ $Centroids$(求树的重心)](https://codeforces.com/contest/1406/problem/C)
> **账号**$10402852@qq.com$ **密码**$m****2$
[USACO10MAR]Great Cow Gathering G
**题目大意**
给你一棵树的结点数$n$和$n-1$条边,你可以删除一条边再增加一条边,使得树的重心唯一
**性质**
① 删除重心后所得的所有子树,节点数不超过原树的$1/2$**一棵树最多有两个重心**
② 树中所有节点到重心的距离之和最小,如果有两个重心,那么他们距离之和相等
③ 两个树通过一条边合并,新的重心在原树两个重心的路径上
④ 树删除或添加一个叶子节点,重心最多只移动一条边
⑤ 一棵树最多有两个重心,且相邻
树的重心定义为树的某个节点,当去掉该节点后,树的各个连通分量中,节点数最多的连通分量其节点数达到最小值。树可能存在多个重心。如下图,当去掉点$1$后,树将分成两个连通块:$(2,4,5)(3,6,7)$,则最大的连通块包含节点个数为$3$。若去掉点$2$,则树将分成$3$个部分,$(4)(5)(1,3,6,7)$最大的连通块包含$4$个节点;第一种方法可以 **得到更小的最大联通分量**。可以发现,其他方案不可能得到比$3$更小的值了。所以,点$1$是树的重心。
![](https://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/BlogImages/202401091307161.png)
**思路**
- 如果找到只有一个重心,那么直接删一个重心的直连边然后加回去就好
- 如果找到两个重心,那么在其中一个重心上找到一个直连点不是另一个重心,删除连另外一个就好
**如何求树的重心?**
1、先任选一个结点作为根节点(比如$1$号节点),把无根树变成有根树。然后设$sz[i]$表示以$i$为根节点的子树节点个数。转移方程为$\displaystyle sz[u]=\sum_{fa[v]=u} (sz[v])$
2、设$son[i]$表示删去节点$x$后剩下的连通分量中最大子树节点个数。其中一部分在原来$i$其为根的子树。$son[i]=max(son[i],siz[i的儿子节点])$;
另外一部分在$i$的“上方”子树有$n-sz[i]$个。
$$son[i]=max(son[i],n-sz[i])$$
![](https://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/BlogImages/202401091310362.png)
```cpp {.line-numbers}
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+100;
typedef long long LL;
vector<LL>g[maxn];
LL siz[maxn],son[maxn];
LL r1,r2,n;
void dfs(LL u,LL fa)
{
siz[u]=1;
son[u]=0;
for(LL i=0;i<g[u].size();i++){
LL v=g[u][i];
if(v==fa) continue;
dfs(v,u);
siz[u]+=siz[v];
son[u]=max(son[u],siz[v]);
}
son[u]=max(son[u],n-siz[u]);
if((son[u]<<1)<=n) r2=r1,r1=u;
}
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
LL t;cin>>t;
while(t--){
cin>>n;
for(LL i=0;i<=n+10;i++) g[i].clear(),siz[i]=0,son[i]=0;
for(LL i=1;i<n;i++){
LL x,y;cin>>x>>y;
g[x].push_back(y);g[y].push_back(x);
}
r1=r2=0;
dfs(1,0);
if(!r2){
LL r3=g[r1][0];
cout<<r1<<" "<<r3<<endl;
cout<<r1<<" "<<r3<<endl;
}
else{
LL r3=r1;
// debug(r1);debug(r2);
for(LL i=0;i<g[r2].size();i++){
r3=g[r2][i];
// debug(r3);
if(r3!=r1) break;
}
cout<<r3<<" "<<r2<<endl;
cout<<r3<<" "<<r1<<endl;
}
}
return 0;
}
```
#### [$P1364$ 医院设置](https://www.luogu.com.cn/problem/P1364)
#### [$P2986$ $Great$ $Cow$ $Gathering$ $G$](https://www.luogu.com.cn/problem/P2986)
https://blog.csdn.net/zstuyyyyccccbbbb/article/details/108952302
CF1324F.Maximum White Subtree

Loading…
Cancel
Save