poj-1655 Balancing Act(树的重心+树形dp)
题目链接:
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 11845 | Accepted: 4993 |
Description
For example, consider the tree:

Deleting node 4 yields two trees whose member nodes are {5} and {1,2,3,6,7}. The larger of these two trees has five nodes, thus the balance of node 4 is five. Deleting node 1 yields a forest of three trees of equal size: {2,6}, {3,7}, and {4,5}. Each of these trees has two nodes, so the balance of node 1 is two.
For each input tree, calculate the node that has the minimum balance. If multiple nodes have equal balance, output the one with the lowest number.
Input
Output
Sample Input
1
7
2 6
1 2
1 4
4 5
3 7
3 1
Sample Output
1 2 题意: 问给的一棵树的重心是哪个节点以及把这个节点去掉后连通块节点个数的最大值; 思路: dfs,找出所有节点的子树节点的个数;再找出去掉这个节点后最大连通块的节点数更新答案就好了; AC代码:
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio> using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=2e5+;
const int maxn=; int n,son[N],ans,num;
vector<int>ve[N]; void dfs(int x,int fa)
{
int len=ve[x].size(),mmax=;
son[x]=;
for(int i=;i<len;i++)
{
int y=ve[x][i];
if(y==fa)continue;
dfs(y,x);
son[x]+=son[y];
if(son[y]>mmax)
mmax=son[y];
} int d=max(mmax,n-son[x]);
if(d<=num)
{
if(d==num)
{
if(x<ans)ans=x;
}
else ans=x;
num=d;
}
} int main()
{
int t;
read(t);
while(t--)
{
read(n);
for(int i=;i<=n;i++)ve[i].clear();
int x,y;
for(int i=;i<n;i++)
{
read(x);read(y);
ve[x].push_back(y);
ve[y].push_back(x);
}
ans=;
num=;
dfs(,-);
cout<<ans<<" "<<num<<"\n";
}
return ;
}
poj-1655 Balancing Act(树的重心+树形dp)的更多相关文章
- POJ 1655 Balancing Act 树的重心
Balancing Act Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. ...
- POJ 1655 - Balancing Act 树型DP
这题和POJ 3107 - Godfather异曲同工...http://blog.csdn.net/kk303/article/details/9387251 Program: #include&l ...
- poj 1655 Balancing Act 求树的重心【树形dp】
poj 1655 Balancing Act 题意:求树的重心且编号数最小 一棵树的重心是指一个结点u,去掉它后剩下的子树结点数最少. (图片来源: PatrickZhou 感谢博主) 看上面的图就好 ...
- POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)
关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...
- POJ 1655.Balancing Act 树形dp 树的重心
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14550 Accepted: 6173 De ...
- POJ 1655 Balancing Act(求树的重心--树形DP)
题意:求树的重心的编号以及重心删除后得到的最大子树的节点个数size,假设size同样就选取编号最小的. 思路:随便选一个点把无根图转化成有根图.dfs一遍就可以dp出答案 //1348K 125MS ...
- poj 1655 Balancing Act(找树的重心)
Balancing Act POJ - 1655 题意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的. /* 找树的重心可以用树形dp或 ...
- POJ 1655 Balancing Act【树的重心】
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14251 Accepted: 6027 De ...
- POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)
树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...
随机推荐
- .NET下 JSON 的一些常用操作
1.JSON的序列化和反序列化 Newtonsoft.Json dll 下载地址http://json.codeplex.com/ using System; using System.Collect ...
- hihoCoder#1196 : 高斯消元·二(开关灯问题)
传送门 高斯消元解异或方程组 小Ho在游戏板上忙碌了30分钟,任然没有办法完成,于是他只好求助于小Hi. 小Ho:小Hi,这次又该怎么办呢? 小Hi:让我们来分析一下吧. 首先对于每一个格子的状态,可 ...
- Linux(1):基本配置
linux里面的网络(网卡)配置: 1. 输出 setup 命令进行设置 2. 选择 "Network configuration" ,按 回车键 3. 选择 "Devi ...
- 全局Session-GlobalSession
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.spri ...
- 深入理解计算机操作系统——第11章:全球IP英特网
全球IP英特网 (1)每台英特网主机都运行实现TCPIP协议的软件. (2)英特网的客户端和服务器混合使用套接字接口函数和Unix IO函数来进行通信. (3)套接字函数典型的是作为陷入内核的系统调用 ...
- msp430项目编程25
msp430中项目---带有断电保护的电子密码锁 1.I2C工作原理 2.I2C通信协议 3.代码(显示部分) 4.代码(功能实现) 5.项目总结 msp430项目编程 msp430入门学习
- 【Java源码】集合类-ArrayList
一.类继承关系 public class ArrayList<E> extends AbstractList<E> implements List<E>, Rand ...
- 分析Linux文件rwx属性的含义
Linux上的文件以.开头的文件被系统视为隐藏文件,仅用ls命令是看不到他们的,而用ls -a除了显示 一般文件名外,连隐藏文件也会显示出来. ls -l(这个参数是字母L的小写,不是数字1) 这个命 ...
- cef3的各个接口你知道几个
CEF3基本的框架包含C/C++程 序接口,通过本地库的接口来实现,而这个库则会隔离宿主程序和 Chromium&Webkit的操作细节.它在浏览器控件和宿主程序之间提供紧密的整合,它支持用户 ...
- C语言必会面试题(3、耶稣有13个门徒,当中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个開始报号:1,2,3,1,2,3...。凡是报到“3”就退出圈子,...)
3.耶稣有13个门徒.当中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个開始报号:1.2,3.1,2,3.... 凡是报到"3"就退出圈子.最后留在圈子 ...