poj 3107 Godfather(树的重心)
来源:互联网 发布:支付宝和淘宝怎么更换 编辑:程序博客网 时间:2024/06/02 05:31
dfs求树的重心。
然后记得用前向星,用vector会t。
然后就是又是惯例的poj过量测试,数组开大一点。
代码:
#include <iostream>#include <cstdio>#include <vector>#include <algorithm>#define ps push_backusing namespace std;const int maxn=5e5+5;//vector<int>edg[maxn];struct node{ int to; int nex;}edg[maxn+10000];int head[maxn];int siz[maxn];int ms[maxn];vector<int>ans;int cnt;void add(int u, int v){ edg[cnt].to=v; edg[cnt].nex=head[u]; head[u]=cnt++;}void dfs(int x, int fa){ siz[x]=ms[x]=0;// for(int i=0; i<(int)edg[x].size(); i++) for(int i=head[x]; i!=-1; i=edg[i].nex) { if(edg[i].to!=fa) { dfs(edg[i].to, x); siz[x]+=siz[edg[i].to]; ms[x]=max(ms[x], siz[edg[i].to]); } } siz[x]++;// cout<<x<<" "<<siz[x]<<endl; return;}int main(){ int x, y, n, i, j; cin>>n; for(i=1; i<=n; i++)head[i]=-1; for(i=0; i<n-1; i++) { scanf("%d%d", &x, &y); add(x, y); add(y, x); } dfs(1, 0); int mi=maxn+10; for(i=1; i<=n; i++) { ms[i]=max(ms[i], n-siz[i]); if(ms[i]<mi) { mi=ms[i]; } } for(i=1; i<=n; i++) { if(ms[i]==mi) { ans.ps(i); } } sort(ans.begin(), ans.end()); printf("%d", ans[0]); for(i=1; i<(int)ans.size(); i++) { printf(" %d", ans[i]); } printf("\n"); return 0;}
阅读全文
0 0
- Poj 3107 Godfather (树的重心)
- POJ 3107 Godfather (树的重心)
- poj 3107 Godfather 求树的重心
- poj 3107 Godfather(树的重心)
- 【POJ 3107 】Godfather 【树的重心】
- poj 3107 Godfather(树形dp,树的重心)
- POJ 题目3107 Godfather(树的重心)
- POJ 3107 Godfather (求树的重心)
- poj 3107 Godfather(树的重心问题)
- POJ.3107 Godfather (树形DP 树的重心)
- POJ 3107 Godfather (树的重心模板题)
- Poj 2599 Godfather(树的重心)
- POJ 3107 Godfather 求重心
- POJ-3107 Godfather & POJ-2378 Tree Cutting(树的重心)
- poj 3107 Godfather 求树的所有重心 树形DP基础题
- POJ3107 Godfather 求树的重心
- POJ3107-树的重心&树形DP-Godfather
- poj3107 Godfather(树的重心)
- Codeforces Translation
- Android开发仿新闻头条上方滑动菜单事件
- java字符串声明
- 刷清橙OJ--A1027.报时助手
- Matlab 对路径的操作及读写文件
- poj 3107 Godfather(树的重心)
- Python基础概念_5_函数
- Cscope How-to
- 指针为何不能修改其指向的常量字符串
- 百度地图自定义icon,定位偏移问题
- 笔记-Kaggle竞赛入门
- java进阶学习计划
- 用代码操纵剪切板
- Linux常用命令(一)--系统命令