思路:
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的更多相关文章

  1. poj1655 Balancing Act 找树的重心

    http://poj.org/problem? id=1655 Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  2. POJ1655 Balancing Act(树的重心)

    题目链接 Balancing Act 就是求一棵树的重心,然后统计答案. #include <bits/stdc++.h> using namespace std; #define REP ...

  3. poj-1655 Balancing Act(树的重心+树形dp)

    题目链接: Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11845   Accepted: 4 ...

  4. poj1655 Balancing Act (dp? dfs?)

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14247   Accepted: 6026 De ...

  5. POJ-1655 Balancing Act

    题目大意:一棵n个节点的树,找出最大子树最小的节点. 题目分析:过程类似求重心. 代码如下: # include<iostream> # include<cstdio> # i ...

  6. poj1655 Balancing Act求树的重心

    Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any nod ...

  7. POJ1655 Balancing Act(树的重心)

    树的重心即树上某结点,删除该结点后形成的森林中包含结点最多的树的结点数最少. 一个DFS就OK了.. #include<cstdio> #include<cstring> #i ...

  8. POJ-1655 Balancing Act(树的重心)

    Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the t ...

  9. POJ1655 Balancing Act (树的重心)

    求树的重心的模板题,size[u]维护以u为根的子树大小,f[u]表示去掉u后的最大子树. 1 #include<cstdio> 2 #include<iostream> 3 ...

随机推荐

  1. 第六节 事务XML方式[声明方式]

    事务管理: 管理事务,管理数据,数据完整性和一致性 事务[业务逻辑] : 由一系列的动作[查询书价格,更新库存,更新余额],组成一个单元[买书业务], 当我们动作当中有一个错了,全错~ ACID 原子 ...

  2. Spark记录-Scala多线程

    Scala多线程 多线程是同时执行多个线程的过程. 它允许您独立执行多个操作.可以通过使用多线程来实现多任务.线程是轻量级的子进程,占用较少的内存.多线程用于在Scala中开发并发应用程序. Scal ...

  3. hdu 4857 Little Devil I

    http://acm.hdu.edu.cn/showproblem.php?pid=4897 题意:给你一棵树,边的颜色要么为白色,要么为黑色,初始每条边为白色,有三种操作 1.将u-v链上面的所有边 ...

  4. 转---一文读懂 python 的元类

    译注:这是一篇在Stack overflow上很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍然觉得 ...

  5. dedecms织梦首页判断,添加不同标题

    <title> {dede:field.title/} {dede:field name='typeid' runphp="yes"}(@me==0)? @me=&qu ...

  6. [转载]Web API OData Inlinecount not working

    http://stackoverflow.com/questions/15422831/web-api-odata-inlinecount-not-working

  7. swift相关文档

    swift官方文档 swift官方文档 https://itunes.apple.com/cn/book/swift-programming-language/id881256329?mt=11 sw ...

  8. Servlet笔记10--Session

    Web编程中的Session: 代码示例: package com.bjpowernode.javaweb.servlet; import java.io.IOException; import ja ...

  9. MySQL 5.6 Replication 复制 FAQ

    原文请参照MySQL官方文档Reference Manual,版本5.6.10. 复制功能使得数据可以从一个MySQL数据库(master主库)复制到另一个或多个MySQL数据库(slave从库).缺 ...

  10. elasticsearch安装kibana插件

    1.下载 2.解压将解压后的文件放到D:\DevTools\kibana-4.6.0-windows-x86路径下 3.修改配置文件D:\DevTools\kibana-4.6.0-windows-x ...