【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 more trees. Define the balance of a node to be the size of the largest tree in the forest T created by deleting that node from T. 
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

The first line of input contains a single integer t (1 <= t <= 20), the number of test cases. The first line of each test case contains an integer N (1 <= N <= 20,000), the number of congruence. The next N-1 lines each contains two space-separated node numbers that are the endpoints of an edge in the tree. No edge will be listed twice, and all edges will be listed.

Output

For each test case, print a line containing two integers, the number of the node with minimum balance and the balance of that node.

Sample Input

1
7
2 6
1 2
1 4
4 5
3 7
3 1

Sample Output

1 2

Source

POJ Monthly--2004.05.15 IOI 2003 sample task

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

题解:本应该快乐的树形DP,和标程没啥区别,甚至连oo都改了差不多了

我就是超时,标程对的,无语,不知道哪错啦。

正解:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAX=20005;

int visit[MAX],num[MAX],head[MAX],ans;
int n,mark,k,sum,sizemin;

struct{int v,next;}e[MAX*2];

void  addedge(int u,int v)
{
    k++;
    e[k].v=v;
    e[k].next=head[u];
    head[u]=k;
    return;
}

void dfs(int u)
{// cout<<"L"<<endl;
    int i,t,min;
    visit[u]=1;
    num[u]=1;min=-1;
    for(i=head[u];i;i=e[i].next)
    {
        t=e[i].v;
        if(!visit[t])
        {
            dfs(t);
            num[u]+=num[t];
            min=max(min,num[t]);
        }
    }
    min=max(min,n-num[u]);
    if(min<sizemin)
    {
        ans=min;
        mark=u;
        sizemin=min;
    }

   return ;
}

int main()
{
    int CASE,u,v,i;
    scanf("%d",&CASE);
    while(CASE--)
    {
        scanf("%d",&n);
        k=0;
        memset(head,0,sizeof(head));
        for(i=1;i<n;i++)
        {
           scanf("%d%d",&u,&v);
           addedge(u,v);
           addedge(v,u);
        }
 // cout<<"no"<<endl;
        memset(num,0,sizeof(num));
        memset(visit,0,sizeof(visit));
        k=0;
        sizemin=MAX+10;
        dfs(1);
        printf("%d %d\n",mark,ans);

    }
    return 0;
}

我的代码,找了一个小时还是错,。。。。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
typedef long long ll;
using namespace std;
const int N=40005;
const int oo=0x3f3f3f3f;
int ycll,n,cnt,head[N],vis[N],num[N],mn,ans,mark;
struct node{
    int v;
    int next;
}e[N];
void add(int u,int v){
    cnt++; e[cnt].v=v;
    e[cnt].next=head[u];
    head[u]=cnt;
}
void dfs(int u){
    vis[u]=1;
    num[u]=1;
    int jjj=-1;
    for(int i=head[u];i;i=e[i].next){
        int dv=e[i].v;
        if(!vis[dv]){
            dfs(dv); num[u]+=num[dv];
            jjj=max(jjj,num[dv]);
        }
    }
    jjj=max(jjj,n-num[u]);
    if(jjj<mn)
       { ans=jjj; mark=u; mn=oo; }
}
int main(){
    freopen("1655.in","r",stdin);
    freopen("1655.out","w",stdout);
    scanf("%d",&ycll);
    while(ycll--){
        scanf("%d",&n);
        cnt=0;
        memset(num,0,sizeof(num));
        memset(vis,0,sizeof(vis));
        memset(head,0,sizeof(head));
        int x,y;
        for(int i=1;i<n;i++){
            scanf("%d %d",&x,&y);
            add(x,y); add(y,x);
        }
        cnt=0; mn=N+10; dfs(1);
        printf("%d %d\n",mark,ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/wuhu-JJJ/p/11241650.html

时间: 2024-08-29 20:42:15

【POJ 1655】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: 10311   Accepted: 4261 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 1741】Tree

Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11570   Accepted: 3626 Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)=The min distance between node u and v. Give an

【POJ 1408】 Fishnet (叉积求面积)

[POJ 1408] Fishnet (叉积求面积) 一个1*1㎡的池塘 有2*n条线代表渔网 问这些网中围出来的最大面积 一个有效面积是相邻两行和相邻两列中间夹的四边形 Input为n 后面跟着四行 每行n个浮点数 每一行分别代表a,b,c,d 如图 并且保证a(i) > a(i-1) b(i) > b(i-1) c(i) > c(i-1) d(i) > d(i-1) n(n <= 30)*2+4(四个岸)条边 枚举点数就行 相邻的四个四个点枚举 找出围出的最大面积 找点用

【POJ 2513】Colored Sticks

[POJ 2513]Colored Sticks 并查集+字典树+欧拉通路 第一次做这么混的题..太混了-- 不过题不算难 字典树用来查字符串对应图中的点 每个单词做一个点(包括重复单词 题意就是每个边走且直走一次(欧拉通路 欧拉图的判定: 没有或者只有两个奇数度的点的图叫做欧拉图 有这些就可以解答此题了 另外需要注意题目范围是25W个木棍 所以最多可能有50W个点 卡了好多个RE 代码如下: #include <iostream> #include <cstdlib> #incl

2292: 【POJ Challenge 】永远挑战

2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 553  Solved: 230[Submit][Status][Discuss] Description lqp18_31和1tthinking经常出题来虐ftiasch.有一天, lqp18_31搞了一个有向图,每条边的长度都是1. 他想让ftiasch求出点1到点 N 的最短路."水题啊.", ftiasch这么说道. 所以1tth

【POJ 1201】 Intervals(差分约束系统)

[POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23817   Accepted: 9023 Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p

【POJ 1228】Grandpa&#39;s Estate 凸包

找到凸包后暴力枚举边进行$check$,注意凸包是一条线(或者说两条线)的情况要输出$NO$ #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define N 1003 #define read(x) x = getint() using namespace std; inline int getint() { int k = 0, fh = 1; char c

【POJ 2750】 Potted Flower(线段树套dp)

[POJ 2750] Potted Flower(线段树套dp) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4566   Accepted: 1739 Description The little cat takes over the management of a new park. There is a large circular statue in the center of the park, surrou