POJ1655 Balancing Act(树的重心)
题目链接 Balancing Act
就是求一棵树的重心,然后统计答案。
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i(0); i < (n); ++i)
#define for_edge(i,x) for(int i = H[x]; i; i = X[i]) const int INF = 1 << 30;
const int N = 100000 + 10; int H[N << 1], E[N << 1], X[N << 1];
int T, et, n, x, y;
int son[N];
int ans_size, ans; inline void addedge(int a, int b){
E[++et] = b, X[et] = H[a], H[a] = et;
E[++et] = a, X[et] = H[b], H[b] = et;
} void dfs(int x, int fa){
int sn = 0;
for_edge(i, x){
int u = E[i];
if (u != fa){
dfs(u, x);
son[x] += son[u];
sn = max(sn, son[u]);
}
}
++son[x];
sn = max(sn, n - son[x]);
if (ans_size > sn || ans_size == sn && ans > x){
ans_size = sn;
ans = x;
} } int main(){ scanf("%d", &T);
for (; T--;){
ans_size = INF; ans = INF;
memset(H, 0, sizeof H);
memset(son, 0, sizeof son);
et = 0;
scanf("%d", &n);
REP(i, n - 1){
scanf("%d%d", &x, &y);
addedge(x, y);
}
dfs(1, 0);
printf("%d %d\n", ans, ans_size);
} return 0; }
POJ1655 Balancing Act(树的重心)的更多相关文章
- poj-1655 Balancing Act(树的重心+树形dp)
题目链接: Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11845 Accepted: 4 ...
- 『Balancing Act 树的重心』
树的重心 我们先来认识一下树的重心. 树的重心也叫树的质心.找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡. 根据树的重心的定义,我们可 ...
- POJ 1655 Balancing Act 树的重心
Balancing Act Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. ...
- poj1655 Balancing Act 找树的重心
http://poj.org/problem? id=1655 Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ1655 Balancing Act(树的重心)
树的重心即树上某结点,删除该结点后形成的森林中包含结点最多的树的结点数最少. 一个DFS就OK了.. #include<cstdio> #include<cstring> #i ...
- POJ-1655 Balancing Act(树的重心)
Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the t ...
- POJ1655 Balancing Act (树的重心)
求树的重心的模板题,size[u]维护以u为根的子树大小,f[u]表示去掉u后的最大子树. 1 #include<cstdio> 2 #include<iostream> 3 ...
- poj1655 Balancing Act (dp? dfs?)
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14247 Accepted: 6026 De ...
- poj3107 求树的重心(&& poj1655 同样求树的重心)
题目链接:http://poj.org/problem?id=3107 求树的重心,所谓树的重心就是:在无根树转换为有根树的过程中,去掉根节点之后,剩下的树的最大结点最小,该点即为重心. 剩下的数的 ...
随机推荐
- 笔记-python-lib-内置函数
笔记-python-lib-内置函数 注:文档来源为Python3.6.4官方文档 1. built-in functions abs(x) 返回绝对值 all(iterable) re ...
- 《Scrum实战》第4次课【全职的Scrum Master】作业汇总
1组: 孟帅 http://www.cnblogs.com/mengshuai1982/p/7375008.html 3组: 张亚辉 http://www.jianshu.com/p/df9eee08 ...
- 处理python字符串中的中文字符
# -*- coding:utf-8 -*- import sys,os txta = open('a.txt','r') str = '' for line in txta: str += line ...
- Linux下测试SSD固态硬盘写入速度
最近买了一个256GB的SSD固态硬盘,想测试一下写入速度,于是如下操作. 部分代码: gettimeofday(&start, NULL); int fd = open("test ...
- ServletConfig和ServletContext 区别
ServletConfig和ServletContext 1.ServletContext在整个web应用程序生命周期内存在,用来保存全局对象,整个web应用都可以使用其获取context参数.当 ...
- REDIS基础笔记
Redis基础笔记 资源链接 简介 简介 安装 五种数据类型及相应命令 1. 字符串类型 2. 散列类型 3. 列表类型 4. 集合类型 5. 有序集合 其他 事务 SORT 生存时间 任务队列 发布 ...
- bable
Babel是一个javascrpt编译器,能将es6转换为es5代码.并且通过插件的形式可以灵活的扩展. 从根目录读取.babelrc文件中读取配置..babelrc是一个json文件 配置 plug ...
- 解决云服务器ECS,windows server 2012不能安装SQL Server 2012,不能安装.NET Fromework 3.5
在云服务器上安装SQL Server 2012 时出现“启用windows功能NetFx3时出错”的问题:NetFx3指的是.NET Framework 3.5,SQL Server 2012数据库系 ...
- [LOJ#2329]「清华集训 2017」我的生命已如风中残烛
[LOJ#2329]「清华集训 2017」我的生命已如风中残烛 试题描述 九条可怜是一个贪玩的女孩子. 这天她在一堵墙钉了 \(n\) 个钉子,第 \(i\) 个钉子的坐标是 \((x_i,y_i)\ ...
- Swift实战-单例模式
设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.GoF提出了23种设计模式,本系列将使用Swift语言来实现这些设计模式 概述 整个应用生命 ...