【POJ 1655】 Balancing Act
【题目链接】
【算法】
树形DP求树的重心
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 20010 int i,T,n,u,v,ans,pos;
vector<int> E[MAXN];
int size[MAXN]; template <typename T> inline void read(T &x) {
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x*= f;
} template <typename T> inline void write(T x) {
if (x < ) { putchar('-'); x = -x; }
if (x > ) write(x/);
putchar(x%+'');
} template <typename T> inline void writeln(T x) {
write(x);
puts("");
} inline void dfs(int x,int fa) {
int i,son,m=;
size[x] = ;
for (i = ; i < E[x].size(); i++) {
son = E[x][i];
if (son != fa) {
dfs(E[x][i],x);
size[x] += size[son];
if (size[son] > m) m = size[son];
}
}
if (n - size[x] > m) m = n - size[x];
if (m < ans) {
ans = m;
pos = x;
}
} int main() { read(T);
while (T--) {
read(n);
for (i = ; i <= n; i++) E[i].clear();
for (i = ; i < n; i++) {
read(u); read(v);
E[u].push_back(v);
E[v].push_back(u);
}
ans = 2e9;
dfs(,);
printf("%d %d\n",pos,ans);
} return ; }
【POJ 1655】 Balancing Act的更多相关文章
- POJ 1655:Balancing Act
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10311 Accepted: 4261 De ...
- 【POJ 1741】Tree
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11570 Accepted: 3626 Description ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
随机推荐
- 2018 ICPC 沈阳网络赛预赛 Supreme Number(找规律)
[传送门]https://nanti.jisuanke.com/t/31452 [题目大意]:给定一个数字(最大可达10100),现在要求不超过它的最大超级质数.超级质数定义:对于一个数,把它看成数字 ...
- STM32F10x_StdPeriph_Driver_3.5.0(中文版).chm的使用
以熟悉的固件库函数说明中函数GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_IintTypeDef *GPIO_InitStructure)为例 GPIOA...G ...
- Android的activity的生命周期
activity的生命周期有四个重要的状态:活动,暂停,停止,销毁 基本的方法有:onCreate.onStart.onResume.onPause.onStop.onDestory 最经常使用的方法 ...
- Linux内存管理之mmap详解 (可用于android底层内存调试)
注:将android底层malloc换为mmap来获取内存,可将获取到的内存添加tag,从而再利用meminfo进行分析,可单独查看该tag的内存,从而进行分析. 一. mmap系统调用 1. mma ...
- 关于Java中强制类型转换的问题
为了更好的理解我们先看下面的例子: package com.yonyou.test; import java.util.ArrayList; import java.util.Iterator; im ...
- Flash/Flex获取外部参数
Part One:Flex程序如何获取html容器传递的URL参数值 我们经常在Flex程序需要用从外部html向swf文件传递参数,(类似 test.html?name=jex&addres ...
- 创建一个zookeeper的会话(实现watcher)
在先前的章节中,我们利用zkCli去了解了一下主要的zookeeper的操作.在接下来的章节中,我们将会学习一下在应用中是怎样利用zookeeper的api的.接下来我们将利用一个程序展示一下,怎样来 ...
- 漫谈程序猿系列:无BUG不生活
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZm9ydW9r/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...
- WCF服务端的.NET Core支持项目Core WCF 正式启动
长期以来在wcf客户端库 https://github.com/dotnet/wcf 里反应最强烈的就是.NET Core的服务端支持 https://github.com/dotnet/wcf/is ...
- IOS------Warning
本文转载至 http://blog.csdn.net/shijiucdy/article/details/8755667 处理警告: 1,Validate Project Settings(updat ...