Balancing Act
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13865   Accepted: 5880

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

 
【题解】
此题用所谓“DP”?求重心即可,更新时时刻注意更新最小节点
一直不理解为啥求重心也叫DP啊!
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib> inline void read(int &x)
{
x = ;char ch = getchar();char c = ch;
while(ch > '' || ch < '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')x = -x;
}
inline int max(int a, int b){return a > b ? a : b;}
inline int min(int a, int b){return a < b ? a : b;} const int INF = 0x3f3f3f3f;
const int MAXN = + ; struct Edge
{
int u,v,next;
}edge[MAXN << ];
int t,n,head[MAXN],cnt,b[MAXN],dp[MAXN],ma,g;
inline void insert(int a, int b){edge[++cnt] = Edge{a,b,head[a]};head[a] = cnt;} void dfs(int u)
{
int pos, tmp = -;
dp[u] = ;
for(pos = head[u];pos;pos = edge[pos].next)
{
int v = edge[pos].v;
if(!b[v])
{
b[v] = true;
dfs(v);
dp[u] += dp[v];
tmp = max(tmp, dp[v]);
}
}
tmp = max(tmp, n - dp[u]);
if(tmp < ma)
ma = tmp,g = u;
if(tmp == ma)
g = min(g, u);
} int main()
{
read(t);
register int i,tmp1,tmp2;
for(;t;--t)
{
ma = INF,g = INF;
memset(edge, , sizeof(edge));
memset(head, , sizeof(head));
cnt = ;memset(dp, , sizeof(dp));
memset(b, , sizeof(b));
read(n);
for(i = ;i < n;++ i)
{
read(tmp1);read(tmp2);
insert(tmp1, tmp2);insert(tmp2, tmp1);
}
b[] = true;
dfs();
printf("%d %d\n", g, ma);
}
return ;
}

POJ1655 Balancing Art的更多相关文章

  1. poj1655 Balancing Act 找树的重心

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

  2. POJ1655 Balancing Act(树的重心)

    题目链接 Balancing Act 就是求一棵树的重心,然后统计答案. #include <bits/stdc++.h> using namespace std; #define REP ...

  3. poj-1655 Balancing Act(树的重心+树形dp)

    题目链接: Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11845   Accepted: 4 ...

  4. poj1655 Balancing Act (dp? dfs?)

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14247   Accepted: 6026 De ...

  5. POJ-1655 Balancing Act

    题目大意:一棵n个节点的树,找出最大子树最小的节点. 题目分析:过程类似求重心. 代码如下: # include<iostream> # include<cstdio> # i ...

  6. [POJ1655]Balancing Act

    思路:DP求树的重心.对于每个结点$i$,$d_i=\displaystyle{\sum_{j\in s(i)}}d_j+1$.删去这个点能得到的最大子树大小即为$\max(\max\limits_{ ...

  7. poj1655 Balancing Act求树的重心

    Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any nod ...

  8. POJ1655 Balancing Act(树的重心)

    树的重心即树上某结点,删除该结点后形成的森林中包含结点最多的树的结点数最少. 一个DFS就OK了.. #include<cstdio> #include<cstring> #i ...

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

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

随机推荐

  1. PHP 类与对象 全解析方法

    1.类与对象 对象:实际存在该类事物中每个实物的个体.$a =new User(); 实例化后的$a 引用�php的别名,两个不同的变量名字指向相同的内容 封装: 把对象的属性和方法组织在一个类(逻辑 ...

  2. odoo:Actions

    actions定义了系统对于用户的操作的响应:登录.按钮.选择项目等. 一:窗口action(ir.actions.act_window ) 最常用的action类型,用于将model的数据展示出来. ...

  3. 在skyline中将井盖、雨水箅子等部件放到地面模型上

    公司三维建模组遇到这样的一个问题,怎样将井盖.雨水盖子恰好放在做好的地面模型上.传统的方法是在skyline中逐个调整井盖的对地高度,就是调整为恰好能放在地面上.或者选择很粗糙的一个方法,在“高度”属 ...

  4. Hibernate继承注解

    hibernate应用中,继承的用途或目的主要有两点: 组件化:故明思义,把重复性的代码抽取成组件,以便重用和维护.hibernate应用中,一些重复的字段,重复的映射配置,就需要抽取成组件. 多态性 ...

  5. IO流16 --- 对象流操作字符串 --- 技术搬运工(尚硅谷)

    序列化 @Test public void test12() throws IOException { ObjectOutputStream oos = new ObjectOutputStream( ...

  6. JavaWeb-类加载器-注解-动态代理

    (一)类加载器 1.什么是类加载器,作用是什么? 类加载器就加载字节码文件(.class) 2.类加载器的种类 类加载器有三种,不同类加载器加载不同的 1)BootStrap:引导类加载器:加载都是最 ...

  7. np.random.choice的用法

    np.random.choice的用法 2018年01月15日 10:18:23 qfpkzheng 阅读数:6306 标签: 自己学习 更多 个人分类: 总结   import numpy as n ...

  8. c/c++输出保留小数

    c语言中,用print可以有格式符号,例如想让a保留两位小数 float a; print( "%.2f", a); 注意这里如果a是0.1, 那么打印出来会自动补0,也就是结果显 ...

  9. Django项目:CRM(客户关系管理系统)--48--39PerfectCRM实现登录+验证码+过期时间+页面保留账号

    # gbacc_urls.py # ————————38PerfectCRM实现全局账号登录注销———————— from django.conf.urls import url from gbacc ...

  10. MVVM 一种新型架构框架

    MVVM是Model-View-ViewModel的简写.微软的WPF带来了新的技术体验,如Silverlight.音频.视频.3D.动画……,这导致了软件UI层更加细节化.可定制化.同时,在技术层面 ...