思路:
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. [转载]10款流行的Markdown编辑器

    10款流行的Markdown编辑器 http://www.csdn.net/article/2014-05-05/2819623 作为一个开源人,如果你不会使用Markdown语法,那你就OUT了!M ...

  2. 20155325 2016-2017-2 《Java程序设计》第8周学习总结

    教材学习内容总结 NIO用于高级输入/输出处理 名称 衔接数据源与目的地 IO InputStream,OutputStream NIO Channel 类 方法 方法的作用 ReadableByte ...

  3. 关于cookie和session

    在设置cookie的时候,它会保留在本地,无论你有没有退出浏览器都是.但是session只能在登录状态有效.退出浏览器过后就会消除掉.同时设置也是有问题的. @app.route('/login',m ...

  4. webpack react 错误整理

    1.ERROR in ./src/entry.js Module build failed: SyntaxError 解决方法: 安装babel-preset-react,  npm install ...

  5. State Estimation for Robotics (Tim Barfoot) exercises Answers

    Here are some exercises answers for State Estimation for Robotics, which I did in June, 2017. The bo ...

  6. Dream------Hbase--0.94版本和0.98/1.X版本api变动

    Dream------Hbase--0.94版本和0.98/1.X版本api变动 网上好多说getQualifier.getValue.getRow被..Array代替了,其实并不是的. 1. Int ...

  7. Struts2不扫描jar包中的action

    今天在做一个二开的项目,将struts打成jar包放在WEB-INF的目录下却扫描不到指定的路径,也就是http访问访问不到我们指定的action,其他代码可以正常使用,就是访问不到action.st ...

  8. python3之SQLAlchemy

    1.SQLAlchemy介绍 SQLAlchemy是Python SQL工具包和对象关系映射器,为应用程序开发人员提供了SQL的全部功能和灵活性. 它提供了一整套众所周知的企业级持久性模式,专为高效和 ...

  9. MySQL灾备恢复在线主从复制变成主主复制及多源复制【转】

    生产主主复制(A<--->B),和灾备主从复制(B--->C).当生产出现问题时,数据写入切换到灾备数据库,待生产恢复后,将灾备回写到生产.步骤如下: 1.灾备与生产其中一台建立主主 ...

  10. 关于阿里云和ucloud云服务器负载均衡器slb和ulb会话保持的配置

    在阿里云slb或者ucloud的ulb上对公司网站后台做了负载均衡以后,发现经常需要重新登录,单独访问没有这样的问题,问题就出在session的保持上,在云控制台中有配置会话的相关选项 阿里云的配置 ...