树形DP 求树的重心,即选择一个结点删去,使得分出的 若干棵树的结点数 的最大值最小

#include<map>
#include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define mod 998244353
#define pi acos(-1)
#define inf 0x7fffffff
#define ll long long
using namespace std;
ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int T,n,ans;
int size[],mx[];
vector<int>e[];
void dp(int x,int fa)
{
size[x]=;
for(int i=;i<e[x].size();i++)
{
int y=e[x][i];
if(y==fa)continue;
dp(y,x);//先算离最远的点
size[x]+=size[y];size(x)表示以x为根除去pre点的树的点的个数
mx[x]=max(mx[x],size[y]);//根据题意要找的是子树里最大的
}
mx[x]=max(mx[x],n-size[x]);
if(mx[x]<mx[ans])ans=x;
if(mx[x]==mx[ans]&&x<ans)ans=x;
}
int main()
{
T=read();
while(T--)
{
n=read();ans=;
mx[]=inf;
for(int i=;i<n;i++)
{
int u=read(),v=read();
e[u].push_back(v);
e[v].push_back(u);
}
dp(,);
printf("%d %d\n",ans,mx[ans]);
for(int i=;i<=n;i++)e[i].clear();
memset(size,,sizeof(size));
memset(mx,,sizeof(mx));
}
return ;
}

poj 1655 找树的重心的更多相关文章

  1. POJ 1655 求树的重心

    POJ 1655 [题目链接]POJ 1655 [题目类型]求树的重心 &题意: 定义平衡数为去掉一个点其最大子树的结点个数,求给定树的最小平衡数和对应要删的点.其实就是求树的重心,找到一个点 ...

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

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

  3. poj 1655 Balancing Act(找树的重心)

    Balancing Act POJ - 1655 题意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的. /* 找树的重心可以用树形dp或 ...

  4. POJ 1655.Balancing Act-树的重心(DFS) 模板(vector存图)

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17497   Accepted: 7398 De ...

  5. Poj 2599 Godfather(树的重心)

    Godfather Time Limit: 2000MS Memory Limit: 65536K Description Last years Chicago was full of gangste ...

  6. POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)

    树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...

  7. poj1655 Balancing Act 找树的重心

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

  8. poj 3107 Godfather(树的重心)

    Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7885   Accepted: 2786 Descrip ...

  9. 求树的重心 poj 1655

    题目链接:https://vjudge.net/problem/POJ-1655 这个就是找树的重心,树的重心就是树里面找一个点,使得以这个点为树根的所有的子树中最大的子树节点数最小.题目应该讲的比较 ...

随机推荐

  1. centos升级glibc2.23编译安装

    wget http://ftp.gnu.org/gnu/glibc/glibc-2.23.tar.gz tar -zxvf glibc-2.23.tar.gz cd glibc-2.23 mkdir ...

  2. Eclipse MAT和jvisualvm分析内存溢出

    ---------------------------------------------mac os版------------------------------------------------ ...

  3. delphi raise 语句: 抛出异常

    //例1:begin  raise Exception.Create('抛出异常');end;//例2:begin  raise Exception.CreateFmt('%s %d', ['错误代码 ...

  4. Element Children

    The childNodes property contains all of the immediate children of the element. There is a significan ...

  5. C语言基础:内置函数的调用

    #include<stdio.h>#include<math.h>#include<stdlib.h>#include<ctype.h>#include ...

  6. 7-1 shell编程基础之二

    shell编程基础之二 算数运算 bash中的算术运算:help let +, -, *, /, %取模(取余), **(乘方),乘法符号有些场景中需要转义 实现算术运算: (1) let var=算 ...

  7. 使用现代 C++ 技术增强多核优化

    在本文中,读者将了解如何使用现代 C++ 技术跨内核并行处理数据.通过研究示例代码,下载应用和学习技术,开发人员将更好地了解英特尔® 架构和多核技术.通过学习如何处理潜在的性能瓶颈和并发性问题,可以使 ...

  8. Jira和confluence备份

    参考: https://www.cnblogs.com/kevingrace/p/8862531.html JIRA备份和还原:  #Jira默认会打开自动备份的功能,备份路径为: /data/atl ...

  9. Array of Doubled Pairs

    Given an array of integers A with even length, return true if and only if it is possible to reorder ...

  10. WSL升级到wsl2并使用strace

    安装wsl的方法,网上很多,一般都ok.这里主要提几个问题: 1.配置securecrt来支持ssh登录. 2.使用strace. 3.使用docker. 由于wsl不支持strace,一直盼着能升级 ...