poj 1655 Balancing Act 寻找树的重心
来源:互联网 发布:centos安装nodejs 编辑:程序博客网 时间:2024/06/01 09:16
就是寻找树的重心的果题。
http://poj.org/problem?id=1655
#include <map>#include <set>#include <queue>#include <cmath>#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;const int MAXN = 20005;const int MAXE = 40005;int N;struct Edge { int to; Edge * next;}*EE,E[MAXE];struct Gragh{ Edge * first;}G[MAXN];int tot;int siz[MAXN];//有多少个子节点int num[MAXN];//子树中最大的int root;void init() { EE = E; memset(E,0,sizeof(E)); memset(G,0,sizeof(G)); num[root=0] = N;}void addedge(int u,int v) { EE->to = v; EE->next = G[u].first; G[u].first = EE++; EE->to = u; EE->next = G[v].first; G[v].first = EE++;}void dfs(int u,int fa = 0) { siz[u] = 1; num[u] = 0; for(Edge * p = G[u].first ; p ; p = p -> next) { if(p->to != fa) { dfs(p->to,u); siz[u] += siz[p->to]; num[u] = max(num[u],siz[p->to]); } } num[u] = max(N-siz[u],num[u]); if(num[u] < num[root]) root = u;}void input() { scanf("%d",&N); init(); int u,v; for(int i = 1 ; i <= N - 1 ; i++) { scanf("%d %d",&u,&v); addedge(u,v); }}void solve() { dfs(1); printf("%d %d\n",root,num[root]);}int main(void) { //freopen("a.in","r",stdin); int T; scanf("%d",&T); while(T--) { input(); solve(); } return 0;}
0 0
- poj 1655 Balancing Act 寻找树的重心
- POJ 1655 Balancing Act【树的重心】
- poj 1655 Balancing Act 【树的重心】
- POJ-1655 Balancing Act 树的重心
- 【POJ】1655 Balancing Act 树的重心
- 【树的重心】 POJ 1655 Balancing Act
- POJ 1655 Balancing Act 树的重心
- 【POJ 1655】Balancing Act 【树的重心】
- POJ 1655 Balancing Act 树的重心
- poj 1655 Balancing Act(树的重心)
- POJ 1655 Balancing Act (树的重心)
- poj 1655 Balancing Act 【树的重心】
- 【树的重心】 POJ 1655 Balancing Act
- POJ 1655 Balancing Act 树的重心
- poj 1655 Balancing Act【树的重心】
- poj 1655 Balancing Act(求树的重心)
- poj 1655 Balancing Act(求树的重心)
- 树的重心学习小记 Poj 1655 Balancing Act
- 类模版1
- freeseal修改屏幕锁和永不修眠
- SDUT3257 & 3258 (素数)
- 数据库中用sql语句进行行列转换
- springMVC使用HandlerMethodArgumentResolver 自定义解析器实现请求参数绑定方法参数
- poj 1655 Balancing Act 寻找树的重心
- 【bzoj3721】 PA2014Final Bazarek 贪心
- web.xml配置跨域
- 《第一行代码Android》学习日记
- Orcale 和 MySql的区别 五十点
- Poi 操作excel 定义单元格颜色
- Linux rpm 命令参数使用详解[介绍和应用]
- Android ActionBar完全解析(续)
- Fragment替换工具类