POJ1655 Balancing Art
Posted 嘒彼小星
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ1655 Balancing Art相关的知识,希望对你有一定的参考价值。
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.
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啊!
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 6 inline void read(int &x) 7 { 8 x = 0;char ch = getchar();char c = ch; 9 while(ch > ‘9‘ || ch < ‘0‘)c = ch, ch = getchar(); 10 while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar(); 11 if(c == ‘-‘)x = -x; 12 } 13 inline int max(int a, int b){return a > b ? a : b;} 14 inline int min(int a, int b){return a < b ? a : b;} 15 16 const int INF = 0x3f3f3f3f; 17 const int MAXN = 200000 + 10; 18 19 struct Edge 20 { 21 int u,v,next; 22 }edge[MAXN << 1]; 23 int t,n,head[MAXN],cnt,b[MAXN],dp[MAXN],ma,g; 24 inline void insert(int a, int b){edge[++cnt] = Edge{a,b,head[a]};head[a] = cnt;} 25 26 void dfs(int u) 27 { 28 int pos, tmp = -1; 29 dp[u] = 1; 30 for(pos = head[u];pos;pos = edge[pos].next) 31 { 32 int v = edge[pos].v; 33 if(!b[v]) 34 { 35 b[v] = true; 36 dfs(v); 37 dp[u] += dp[v]; 38 tmp = max(tmp, dp[v]); 39 } 40 } 41 tmp = max(tmp, n - dp[u]); 42 if(tmp < ma) 43 ma = tmp,g = u; 44 if(tmp == ma) 45 g = min(g, u); 46 } 47 48 int main() 49 { 50 read(t); 51 register int i,tmp1,tmp2; 52 for(;t;--t) 53 { 54 ma = INF,g = INF; 55 memset(edge, 0, sizeof(edge)); 56 memset(head, 0, sizeof(head)); 57 cnt = 0;memset(dp, 0, sizeof(dp)); 58 memset(b, 0, sizeof(b)); 59 read(n); 60 for(i = 1;i < n;++ i) 61 { 62 read(tmp1);read(tmp2); 63 insert(tmp1, tmp2);insert(tmp2, tmp1); 64 } 65 b[1] = true; 66 dfs(1); 67 printf("%d %d\n", g, ma); 68 } 69 return 0; 70 }
以上是关于POJ1655 Balancing Art的主要内容,如果未能解决你的问题,请参考以下文章