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 题目大意: 直接给你一棵树,让你求树的 ...
随机推荐
- 异常 Failed to bind NettyServer on /10.133.7.216:29105, cause: Failed to bind to: /0.0.0.0:29105
"C:\Program Files\Java\jdk1.7.0_80\bin\java" -agentlib:jdwp=transport=dt_socket,address=12 ...
- 一个python爬虫协程的写法(gevent模块)
from bs4 import BeautifulSoup import requests import gevent from gevent import monkey, pool monkey.p ...
- BZOJ1739: [Usaco2005 mar]Space Elevator 太空电梯
n<=400个东西,每个东西有高度<=100,这种东西在堆放过程中不得超过的最大高度<=40000,以及每个东西的个数<=10,求最高能堆多高. 算了下背包复杂度不太对然后开了 ...
- BZOJ1696: [Usaco2007 Feb]Building A New Barn新牛舍
n<=10000个点(xi,yi),找到一个不同于给出的所有点的点,使得该点到所有点的曼哈顿距离最小并找出这样的点的个数. 第一眼看上去这不是中位数嘛,奇数一个点偶数一片,然后找一下这篇区域有几 ...
- git修改commit message及vi编辑器的简单使用
1.修改commit信息 git commit --amend 2.进入vi编辑器修改 ‘i’进入insert模式,输入文字: ‘esc’回到命令模式,删除文字,移动光标: ‘:’进入底行模式,‘wq ...
- vm 安装CentOS7
1.首先需要到CentOS官网下载CentOS7的iso镜像文件,地址http://mirrors.cn99.com/centos/7/isos/x86_64/ 这里我选择的是迅雷种子文件 2.下载完 ...
- leetCode 78.Subsets (子集) 解题思路和方法
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- win8系统 重装系统如何删除EFI分区
在PE下(一般重装系统就是在PE下),依次输入如下命令(注意虽然显示的是中文名"磁盘0",但是还是用英文disk 0) list disk select disk 0 clean
- VC++ error C1083 无法打开包括文件 fstream.h,iostream.h怎么办
1 如下图所示,VS中不支持iostream.h和fstream.h的说法 2 改成下面三行就可以编译通过了 #include<iostream> #include <fstre ...
- SqlServer 经常使用分页方法总结
SqlServer 经常使用分页方法总结 以下演示样例总结了,SqlServer数据库 经常使用分页方法,仅供学习參考 A. 使用 RowNumber 和 Between And 组合分页: /*** ...