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. mysql复制关系

    一旦建立好主从复制关系后就不要在从库上执行任何dml和ddl操作,包括创建用户也不行. 那么万一在从库上执行了dml或者ddl操作了,会有何影响,以及如何恢复? slave同步状态中出现Slave_S ...

  2. anchor-free : CornerNet 和 CenterNet 简要笔记

    CornerNethourglass network -> prediction module = corner pooling -> heatmaps + embedding + off ...

  3. 获取地址栏URL中参数, getQuerySting()方法

    今天同事用的以前的获取url地址参数获取不到.以前的方法失效了.后面发现是正则表达式bug: 第一种获取方法(针对普通情况的一般够用): function getQueryString(name) { ...

  4. Python移动自动化测试面试

    Python移动自动化测试面试 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大家看的时候可以关 ...

  5. npm config set registry 与 cnpm的区别

    一直以为这2个事没有区别的: npm config set registry http://registry.npm.taobao.org 后使用npm命令. npm install -g cnpm ...

  6. stringstream的使用 UVA 10815

    水题题目描述就不写了 主要是发现stringstream真的是好用,可以把string绑定到stringstream中,然后就能以空格为分隔符分割出每个单词,听说每次重新创建stringstream开 ...

  7. Luogu P2679 子串(字符串+dp)

    P2679 子串 题意 题目描述 有两个仅包含小写英文字母的字符串\(A\)和\(B\). 现在要从字符串\(A\)中取出\(k\)个互不重叠的非空子串,然后把这\(k\)个子串按照其在字符串\(A\ ...

  8. Leetcode405Convert a Number to Hexadecimal数字转换为十六进制数

    给定一个整数,编写一个算法将这个数转换为十六进制数.对于负整数,我们通常使用 补码运算 方法. 注意: 十六进制中所有字母(a-f)都必须是小写. 十六进制字符串中不能包含多余的前导零.如果要转化的数 ...

  9. RDS for PostgreSQL 云盘加密功能使用方法

    RDS for PostgreSQL支持数据盘的加密功能,提供更高的数据存储安全等级.(加密KEY可以由用户提供,也可以由KMS服务生成.) 使用方法: 1.选中需要购买数据库实例的region,创建 ...

  10. LUOGU P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...