POJ1655 Balancing Act(树的重心)

题意:

给你一棵树,求树的重心

如果有多个就输出序号最小的

思路:

树的重心就是以它为根的所有子树中节点最多的节点数最小

树形dp轻松可以解决

/* ***********************************************
Author        :devil
************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <stdlib.h>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int N=2e4+10;
int n,sum[N],ans,p;
bool vis[N];
vector<int>eg[N];
void dfs(int u)
{
    int ma=0;
    vis[u]=1;
    for(int i=0;i<eg[u].size();i++)
    {
        int v=eg[u][i];
        if(vis[v]) continue;
        dfs(v);
        sum[u]+=sum[v]+1;
        ma=max(ma,sum[v]+1);
    }
    ma=max(ma,n-sum[u]-1);
    if(ma<ans||ma==ans&&u<p)
    {
        ans=ma;
        p=u;
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    int t,x,y;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        ans=inf;
        for(int i=1;i<=n;i++)
        {
            eg[i].clear();
            sum[i]=0;
            vis[i]=0;
        }
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            eg[x].push_back(y);
            eg[y].push_back(x);
        }
        dfs(1);
        printf("%d %d\n",p,ans);
    }
    return 0;
}
时间: 2024-08-07 03:06:58

POJ1655 Balancing Act(树的重心)的相关文章

POJ1655 Balancing Act(树的重心)

题目链接 Balancing Act 就是求一棵树的重心,然后统计答案. 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define REP(i,n) for(int i(0); i < (n); ++i) 6 #define for_edge(i,x) for(int i = H[x]; i; i = X[i]) 7 8 const int INF = 1 << 30; 9 const int N = 10

『Balancing Act 树的重心』

树的重心 我们先来认识一下树的重心. 树的重心也叫树的质心.找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡. 根据树的重心的定义,我们可以通过树形DP来求解树的重心. 设\(Max_i\)代表删去i节点后树中剩下子树中节点最多的一个子树的节点数.由于删去节点i至少将原树分为两部分,所以满足\(\ \frac{1}{2} \leq Max_i\),我们要求的就是一个\(i\),使得\(Max_i\)最小. 对于Max数组,我们可以列出

POJ 1655 Balancing Act[树的重心/树形dp]

Balancing Act 时限:1000ms 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 more trees. Define the balance of a node to be the size of the largest tree

POJ 1655 Balancing Act 树的重心 基础题

Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10347   Accepted: 4285 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 (树的重心)

题目地址:POJ 1655 树的重心定义为:找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡. 树的重心可以用树形DP快速的找出来. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h>

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

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

poj1655 Balancing Act 求树的重心

http://poj.org/problem?id=1655 Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9072   Accepted: 3765 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a fo

poj1655 Balancing Act(找树的重心)

Balancing Act POJ - 1655 题意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的. /* 找树的重心可以用树形dp或者dfs,这里我用的dfs 基本方法就是随便设定一个根节点,然后找出这个节点的子树大小(包括这个节点),然后总点数减去子树的大小就是向父亲节点走去的点数,使这几部分的最大值最小 */ #include<iostream> #include<cstdio> #include<alg

poj1655 Balancing Act (dp? dfs?)

Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14247   Accepted: 6026 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