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. axios post请求后台接收不到参数 和 一些配置问题

    原因: axios 的 headers的 content-type  默认是 “application/json ”,传给后台的格式是这样的: 但是后台接收数据的格式一般是表单格式的,就是formda ...

  2. extern “C”的用法

    引言 由于不同的代码互相调用起来很容易出错,甚至同一种代码但由不同的编译器编译,为实现C++代码调用其他C语言代码,会在C语言代码的部分加上extern "C",表明这段代码需要按 ...

  3. Yaf--个人封装yaf的框架+swoole+elasticsearch(Window+linux版)

    这是基于c写底层的yaf框架集成PDO+predis+读写分离+composer+全局异常处理+多模块开发+Log日志记录简单容易上手的框架 注意:window版没有swoole和Smarty主要用作 ...

  4. vue项目打包部署到服务器,静态资源文件404

    js文件404问题        原因:打包的项目静态资源的路径需要设置为绝对路径.如果是相对路径会出错 解决办法:修改config/index.js文件,将 assetsPublicPath修改为' ...

  5. xshell 连接 kali

    1   修改配置文件 vi /etc/ssh/sshd_config #PasswordAuthentication no 去掉#,并且将no修改为YES //kali中默认是yes PermitRo ...

  6. IndentationError: expected an indented block错误

    Python语言是一款对缩进非常敏感的语言,给很多初学者带来了困惑,即便是很有经验的python程序员,也可能陷入陷阱当中.最常见的情况是tab和空格的混用会导致错误,或者缩进不对,而这是用肉眼无法分 ...

  7. html 输入框显示“小叉叉”的清空方法

    在IE10以下,我们的输入框input会出现小叉叉.怎么解决这个问题呢? 针对input框我们做一个处理 <style type="text/css"> input:: ...

  8. nslookup获取域名对应的的ip地址

    1.先用nslookup获得域名对应的主机ip:nslookup 域名 2.再根据主机ip获得对应的主机名称: host ip $ nslookup baidu.com Server: 127.0.1 ...

  9. Django项目:CRM(客户关系管理系统)--27--19PerfectCRM实现King_admin数据修改

    登陆密码设置参考 http://www.cnblogs.com/ujq3/p/8553784.html {#table_data_list.html#} {## ————————08PerfectCR ...

  10. 【noip】跟着洛谷刷noip题2

    noip好难呀. 上一个感觉有点长了,重开一个. 36.Vigenère 密码 粘个Openjudge上的代码 #include<cstdio> #include<iostream& ...