[POJ1655]Balancing Act
思路:
DP求树的重心。
对于每个结点$i$,$d_i=\displaystyle{\sum_{j\in s(i)}}d_j+1$。
删去这个点能得到的最大子树大小即为$\max(\max\limits_{j\in s(i)}\{d(j)\},n-d(i))$。
然后取最小的结点即可。
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
inline int getint() {
char ch;
while(!isdigit(ch=getchar()));
int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int inf=0x7fffffff;
const int V=;
std::vector<int> e[V];
inline void add_edge(const int u,const int v) {
e[u].push_back(v);
}
int id,minsize,size[V];
inline void init() {
for(int i=;i<V;i++) e[i].clear();
id=minsize=inf;
memset(size,,sizeof size);
}
int n;
void DFS(const int x,const int par) {
size[x]=;
int max=;
for(unsigned i=;i<e[x].size();i++) {
int &y=e[x][i];
if(y==par) continue;
DFS(y,x);
size[x]+=size[y];
max=std::max(max,size[y]);
}
max=std::max(max,n-size[x]);
if(max<minsize) {
minsize=max;
id=x;
}
else if(max==minsize&&x<id) {
id=x;
}
}
int main() {
for(int T=getint();T;T--) {
init();
n=getint();
for(int i=;i<n;i++) {
int u=getint(),v=getint();
add_edge(u,v);
add_edge(v,u);
}
DFS(,);
printf("%d %d\n",id,minsize);
}
return ;
}
[POJ1655]Balancing Act的更多相关文章
- poj1655 Balancing Act 找树的重心
http://poj.org/problem? id=1655 Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ1655 Balancing Act(树的重心)
题目链接 Balancing Act 就是求一棵树的重心,然后统计答案. #include <bits/stdc++.h> using namespace std; #define REP ...
- poj-1655 Balancing Act(树的重心+树形dp)
题目链接: Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11845 Accepted: 4 ...
- poj1655 Balancing Act (dp? dfs?)
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14247 Accepted: 6026 De ...
- POJ-1655 Balancing Act
题目大意:一棵n个节点的树,找出最大子树最小的节点. 题目分析:过程类似求重心. 代码如下: # include<iostream> # include<cstdio> # i ...
- poj1655 Balancing Act求树的重心
Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any nod ...
- 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 ...
随机推荐
- service注入到action中
service注入到action中 之前本人每次要获得service都是在action自己通过WebApplicationContext的getBean获得的,一直在spring中只配置到了servi ...
- 【Linux】MySQL安装及允许远程访问
安装环境/工具 Linux( centOS 版) MySQL(MySQL-5.6.28-1.el7.x86_64.rpm-bundle.tar版) 安装步骤 1.解压mysql安装文件 命令:tar ...
- bzoj千题计划295:bzoj3140: [Hnoi2013]消毒
http://www.lydsy.com/JudgeOnline/problem.php?id=3140 如果只有两维,那就是二分图最小点覆盖 现在是三维,但是a*b*c<=5000,说明最小的 ...
- bzoj千题计划227:bzoj1486: [HNOI2009]最小圈
http://www.lydsy.com/JudgeOnline/problem.php?id=1486 二分答案 dfs版spfa判负环 #include<queue> #include ...
- scala 基础到高阶
本文打算对这小段时间学习 scala 以及 spark 编程技术做个小结,一来温故而知新,而来为以后查阅方便 spark scala 入门小例子 文本文件 UserPurchaseHistory.c ...
- Java SSM框架之MyBatis3(一)MyBatis入门
MyBatis3介绍 mybatis就是一个封装来jdbc的持久层框架,它和hibernate都属于ORM框架,但是具体的说,hibernate是一个完全的orm框架,而mybatis是一个不完全的o ...
- [原]Android开发优化-Adapter优化
ListView作为Android开发中使用频率最高的一个控件,保证ListView的流畅运行,对用户体验的提高至关重要.Adapter是ListView和数据源之间的中间人,当每条数据进入可见区时, ...
- Linux 网卡流量查看
网卡流量查看 watch more /proc/net/dev # 实时监控流量文件系统 累计值 iptraf # 网卡流量查看工具 nethogs -d 5 eth0 eth1 # 按进程实时统计网 ...
- innodb和myisam数据库文件存储详解以及mysql表空间
数据库常用的两种引擎有Innodb和Myisam,关于二者的区别参考:https://www.cnblogs.com/qlqwjy/p/7965460.html 1.关于数据库的存储在两种引擎的存储是 ...
- 【黑客免杀攻防】读书笔记10 - switch-case分支
0x1 switch-case分支 switch-case其实就是if-else语句的另一种体现形式.但大于3之后的switchc-case.编译器会对代码进行优化. 1.1 简单switch-cas ...