当前位置: 首页 > news >正文

AcWing 846:树的重心 ← 链式前向星 or 邻接表

【题目来源】
https://www.acwing.com/problem/content/848/

【问题描述】
给定一颗树,树中包含 n 个结点(编号 1∼n)和 n−1 条无向边。
请你找到树的重心,并输出将重心删除后,剩余各个连通块中点数的最大值。
重心定义:重心是指树中的一个结点,如果将这个点删除后,剩余各个连通块中点数的最大值最小,那么这个节点被称为树的重心。

【输入格式】
第一行包含整数 n,表示树的结点数。
接下来 n−1 行,每行包含两个整数 a 和 b,表示点 a 和点 b 之间存在一条边。

【输出格式】
输出一个整数 m,表示将重心删除后,剩余各个连通块中点数的最大值。

【数据范围】
1≤n≤10^5

【输入样例】
9
1 2
1 7
1 4
2 8
2 5
4 3
3 9
4 6

【输出样例】
4

【算法分析】
● 链式前向星:https://blog.csdn.net/hnjzsyjyj/article/details/139369904
● 邻接表存“树”:https://blog.csdn.net/hnjzsyjyj/article/details/108655516

【算法代码一:链式前向星

#include <bits/stdc++.h>
using namespace std;const int N=1e5+10;
const int M=N<<1;
int h[N],e[M],ne[M],idx;
bool st[N];
int imax=INT_MAX;
int n;void add(int a,int b) {e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}int dfs(int u) {st[u]=true;int cnt=1;int rem=0; //remnantfor(int i=h[u]; i!=-1; i=ne[i]) {int j=e[i];if(!st[j]) {int t=dfs(j);rem=max(rem,t);cnt+=t;}}rem=max(rem,n-cnt);imax=min(imax,rem);return cnt;
}int main() {cin>>n;memset(h,-1,sizeof(h));for(int i=1; i<n; i++) {int a,b;cin>>a>>b;add(a,b),add(b,a);}dfs(1);cout<<imax<<endl;return 0;
}/*
in:
9
1 2
1 7
1 4
2 8
2 5
4 3
3 9
4 6out:
4
*/

【算法代码二:邻接表

#include<bits/stdc++.h>
using namespace std;const int N=1e5+5;
vector<int> G[N];
bool st[N];
int imax=INT_MAX;
int n;int dfs(int u) {st[u]=true;int cnt=1;int rem=0; //remnantfor(auto i:G[u]) {if(!st[i]) {int t=dfs(i);rem=max(rem,t);cnt+=t;}}rem=max(rem,n-cnt);imax=min(rem,imax);return cnt;
}int main() {cin>>n;int x,y;for(int i=1; i<n; i++) {cin>>x>>y;G[x].push_back(y);G[y].push_back(x);}dfs(1);cout<<imax<<endl;return 0;
}/*
in:
9
1 2
1 7
1 4
2 8
2 5
4 3
3 9
4 6out:
4
*/

 【算法代码三:链式前向星
此代码与“东方博宜OJ 2190:树的重心”代码的差别,仅在于最后一部分的循环内容不同。
详见:https://blog.csdn.net/hnjzsyjyj/article/details/155821553

#include <bits/stdc++.h>
using namespace std;const int N=1e5+5;
const int M=N<<1;
int h[N],e[M],ne[M],idx;
int pre[N],cnt[N];
int rem[N]; //remnant
int n;void add(int a,int b) {e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}int dfs(int u,int fa) {cnt[u]=1;pre[u]=fa;for(int i=h[u]; i!=-1; i=ne[i]) {int j=e[i];if(j!=fa) cnt[u]+=dfs(j,u);}return cnt[u];
}int main() {memset(h,-1,sizeof h);cin>>n;for(int i=1; i<n; i++) {int a,b;cin>>a>>b;add(a,b),add(b,a);}dfs(1,-1);int imin=INT_MAX;for(int u=1; u<=n; u++) {rem[u]=n-cnt[u];for(int i=h[u]; i!=-1; i=ne[i]) {int j=e[i];if(pre[j]==u) {rem[u]=max(rem[u],cnt[j]);}}imin=min(imin,rem[u]);}int imax=INT_MIN;for(int u=1; u<=n; u++) {if(imin==rem[u]) {int t=0;for(int i=h[u]; i!=-1; i=ne[i]) {int j=e[i];if(pre[j]==u) {t=max(t,cnt[j]);}}t=max(t,n-cnt[u]);imax=max(imax,t);}}cout<<imax<<endl;return 0;
}/*
in:
10
1 2
1 3
4 2
4 5
3 6
7 6
8 6
9 6
7 10out:
5
*/





【参考文献】
https://blog.csdn.net/hnjzsyjyj/article/details/155821553
https://www.acwing.com/solution/content/287387/
https://blog.csdn.net/hnjzsyjyj/article/details/139369904
https://blog.csdn.net/hnjzsyjyj/article/details/108655516
https://blog.csdn.net/qq_47783057/article/details/116195481
https://www.acwing.com/solution/content/13513/
https://www.acwing.com/solution/content/4917/

 

 

http://www.jsqmd.com/news/73193/

相关文章:

  • PyMe是一款面向大众的可视化低代码Python开发工具
  • 251211
  • 2025最新广州创业瑜伽培训中心TOP5评测!优质瑜伽馆年度盘点,专业传承+实战赋能权威榜单发布,引领都市身心疗愈新生态 - 全局中转站
  • Python自然语言处理的未来:技术栈与开发范式
  • AI元人文构想:技术标准与人文规范的统一——拥抱数值表征vs审慎价值优化
  • Ubuntu系统火狐浏览器配置http代理
  • 1-Year XTOOL D9S PRO Update: Latest Diagnostics for European/American Mechanics Car Owners
  • 2025年目前有名的门窗源头厂家口碑推荐,复古门窗/中式门窗/整体门窗/极简门窗/高端定制门窗/智能门窗/门窗品牌有哪些 - 品牌推荐师
  • 2025年苏州GEO排名产品TOP10,本地企业表现亮眼,企业短视频矩阵/ai排行榜/GEO排名/短视频矩阵/视频矩阵GEO排名厂商排行榜 - 品牌推荐师
  • 2025年12月上海简约装修,上海中古风装修,上海工业风装修公司推荐,高颜值与实用性兼具的优质品牌 - 品牌鉴赏师
  • 2025年12月上海简约装修,上海中古风装修,上海工业风装修公司推荐,高颜值与实用性兼具的优质品牌 - 品牌鉴赏师
  • 关于公司经营场所和股东变更的公告 - 指南
  • 观察者模式
  • 2025年东莞优质的铝门窗批发选哪家,安全门窗/铝门窗/慕莎尼奥门窗/窗纱一体铝门窗/门窗/铝门窗品牌选哪家 - 品牌推荐师
  • 2025年市面上排行前列的推拉窗厂家有哪些,侧压平移推拉窗/六轨断桥推拉窗/平移断桥提升窗/推拉窗源头厂家推荐排行榜 - 品牌推荐师
  • 详细介绍:Chrome HSTS(HTTP Strict Transport Security)
  • tmux使用教程
  • 2025年12月上海别墅装修,上海极简风装修,上海新中式装修公司权威推荐,设计实力与市场口碑深度解析 - 品牌鉴赏师
  • Python 编程实战 实用工具与库 — Django 项目结构简介 - 指南
  • 2025.12.11总结
  • 2025年短视频代运营口碑前十强,专业机构推荐,短视频代运营团队/小红书代运营/短视频代运营/企业号代运营短视频代运营系统选哪家 - 品牌推荐师
  • 124_尚硅谷_闭包的基本介绍
  • One Year XTOOL D9S Update Service: Keep Diagnostics Up-to-Date for EU US Vehicles
  • 如何确定arm固件的加载地址
  • 2025年数控车床品牌新格局,机械手集成能力排行揭晓,动力刀塔数控车/牙科配件数控车床/新能源数控车床/军工配件数控机床数控车床设计怎么选择 - 品牌推荐师
  • 2025年国内靠谱的门窗源头厂家推荐,全屋门窗/环保门窗/复古门窗/极简门窗/欧式门窗/智能门窗/门窗直销厂家找哪家 - 品牌推荐师
  • 2025军工智造基石:十大数控车床厂家权威评测排行,4轴数控机床/水暖接头数控机床/机械手数控车床/液冷接头数控机床数控车床门店怎么选择 - 品牌推荐师
  • 1-Year XTOOL D9 Update: Keep Diagnostics Current for EU US Vehicles
  • <<Learning visionbased agile flight via differentiable physics>>端到端无人机高速避障方案解析 - 教程
  • 2025年机械手数控车床品牌价格排行:前十名多少钱一台?英伟达液冷数控车/车铣复合数控机床/医疗器械数控机床数控车床采购排行榜 - 品牌推荐师