【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 ...
随机推荐
- oracle内核学习总结
http://blog.csdn.net/bcbobo21cn/article/category/3092145/1
- 基于R-Tree的最近邻查询
转自基于R-Tree的最近邻查询 BAB(Branch.and.Band)算法是由Nick Roussopoulousnl等人于1995年提出的,是最早的基于R.树的静态最近邻查询算法.该算法使用MI ...
- outlook 2010 自动密送Email
以下功能请勿非法使用: 密抄到多人这个需要用到宏 方法一: 1.在Outlook里面键入ALT+F11打开VBA编辑器 2.展开“Project (VbaProject.OTM)/Microsoft ...
- ``Accordian'' Patience
``Accordian'' Patience You are to simulate the playing of games of ``Accordian'' patience, the rule ...
- oracle 11G direct path read 非常美也非常伤人
direct path read 在11g中,全表扫描可能使用direct path read方式,绕过buffer cache,这种全表扫描就是物理读了. 在10g中,都是通过gc buffer来读 ...
- eclipse svn org.tigris.subversion.javahl.ClientException: RA layer request failed
突然之间eclipse使用svn更新项目时报错,org.tigris.subversion.javahl.ClientException: RA layer request failed 网上搜的都是 ...
- openwrt hotplug
由内核发出 event 事件. kobject_uevent() 产生 uevent 事件(lib/kobject_uevent.c 中), 产生的 uevent 先由 netlink_broadca ...
- java使用默认线程池踩过的坑(三)
云智慧(北京)科技有限公司 陈鑫 重新启动线程池 TaskManager public class TaskManager implements Runnable { -.. public TaskM ...
- npm WARN uninstall not installed in /Users/hrt0kmt/node_modules: "xxx"
You may meet this error on home directory. % npm uninstall appium npm WARN uninstall not installed i ...
- Tomcat配置,Myeclipse破解和各种设置
转自:http://www.cnblogs.com/tyjsjl/archive/2006/11/14/2156111.html 根据tomcat来配置eclipse和MyEclipse结合使用起来, ...