【poj1655】Balancing Act

http://poj.org/problem?id=1655 (题目链接)

题意:给出一棵树,求树上一点使得使得删除一点后该树的最大子树最小。

solution 
  树的重心裸题。 
  随意取一点作为根节点,dfs维护当前节点的最大子树大小以及它的父亲子树大小,更新答案。

代码:

// poj1655
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std;

const int maxn=20010;
struct edge {int to,next;}e[maxn<<2];

int head[maxn],vis[maxn],son[maxn],cnt,size,ans,n;

void insert(int u,int v) {
    e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;
    e[++cnt].to=u;e[cnt].next=head[v];head[v]=cnt;
}
void dfs(int x) {
    vis[x]=1,son[x]=0;
    int tmp=0;
    for (int i=head[x];i;i=e[i].next) if (!vis[e[i].to]) {
            dfs(e[i].to);
            son[x]+=son[e[i].to]+1;
            tmp=max(tmp,son[e[i].to]+1);
        }
    tmp=max(tmp,n-son[x]-1);
    if (tmp<size || (tmp==size && x<ans)) size=tmp,ans=x;
}
int main() {
    int T;scanf("%d",&T);
    while (T--) {
        scanf("%d",&n);
        memset(vis,0,sizeof(vis));memset(head,0,sizeof(head));
        cnt=ans=0;size=inf;
        for (int i=1;i<n;i++) {
            int u,v;
            scanf("%d%d",&u,&v);
            insert(u,v);
        }
        dfs(1);
        printf("%d %d\n",ans,size);
    }
    return 0;
}

  

时间: 2024-07-31 18:29:48

【poj1655】Balancing Act的相关文章

【POJ 1655】Balancing Act 【树的重心】

Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9241   Accepted: 3846 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or mo

【POJ 1655】Balancing Act

Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18593   Accepted: 7849 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or m

【poj1655】【poj3107】【求树的重心】

先来说一下怎样来求树的直径: 假设 s-t这条路径为树的直径,或者称为树上的最长路 现有结论,从任意一点u出发搜到的最远的点一定是s.t中的一点,然后在从这个最远点开始搜,就可以搜到另一个最长路的端点,即用两遍广搜就可以找出树的最长路 证明: 1 设u为s-t路径上的一点,结论显然成立,否则设搜到的最远点为T则 dis(u,T) >dis(u,s) 且 dis(u,T)>dis(u,t) 则最长路不是s-t了,与假设矛盾 2 设u不为s-t路径上的点 首先明确,假如u走到了s-t路径上的一点,

【poj3107】 Godfather (树的重心)

Description Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all these crimes, and decided to arrest the mafia leaders. Unfortunately, the structure of Chicago mafia is rather complicated

poj1655 Balancing Act 【树形DP(很弱)】

都不知道怎么分类了. 大概要求一个树中以某个结点为根的子树结点个数,还有儿子结点中以儿子结点为根的子树结点个数的最大值,用递归得到n[i],以i为根节点的子树结点个数 #include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> #include <vector> #include <cstring> #include <cmath&g

poj 1655 Balancing Act 【树的重心】

知识点:树的重心 定义:以这个点为根,那么所有的子树(不算整个树自身)的大小都不超过整个树大小的一半. 性质: 性质 1 :树中所有点到某个点的距离和中,到重心的距离和是最小的,如果有两个距离和,他们的距离和一样. 性质 2 :把两棵树通过某一点相连得到一颗新的树,新的树的重心必然在连接原来两棵树重心的路径上. 性质 3 :一棵树添加或者删除一个节点,树的重心最多只移动一条边的位置. 题目:poj 1655 Balancing Act 题意:给出一颗树,求树的重心点以及重心点删除后中的最大子树.

POJ 1655 Balancing Act【树的重心】

Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14251   Accepted: 6027 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or m

poj 1655 Balancing Act 求树的重心【树形dp】

poj 1655 Balancing Act 题意:求树的重心且编号数最小 一棵树的重心是指一个结点u,去掉它后剩下的子树结点数最少. (图片来源: PatrickZhou 感谢博主) 看上面的图就好明白了,不仅要考虑当前结点子树的大小,也要"向上"考虑树的大小. 那么其它就dfs完成就行了,son[] 存以前结点为根的结点个数. 这是用邻接表写: 1 #include<iostream> 2 #include<cstdio> 3 #include<cst

使用Application Center Test (ACT)来做压力测试 【转】

在我们完成了基于SPS2003的开发,实现了我们的具体应用以后,我们是不是就可以直接请用户来使用了呢?如果我这么做,那么有经验的开发人员一定会对此嗤之以鼻:居然连压力测试也不做!真是不想活了…… 呵呵,是啊.开发环境往往只考虑功能,到了具体环境中,就需要考虑有大量的用户来访问的时候,很多功能会不会出错?性能会怎么样呢?……我们这里就简单看看,怎么来做压力测试. 相信作压力测试肯定有很多工具,而我们一般使用的,现在很多是Application Center Test (ACT).这个东东是VS.N