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.

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

【题解】

此题用所谓“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 }

时间: 2024-08-03 10:07:19

POJ1655 Balancing Art的相关文章

poj1655 Balancing Act 【树形DP(很弱)】

都不知道怎么分类了. 大概要求一个树中以某个结点为根的子树结点个数,还有儿子结点中以儿子结点为根的子树结点个数的最大值,用递归得到n[i],以i为根节点的子树结点个数 #include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> #include <vector> #include <cstring> #include <cmath&g

poj1655 Balancing Act 求树的重心

http://poj.org/problem?id=1655 Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9072   Accepted: 3765 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a fo

POJ1655 Balancing Act(树的重心)

题目链接 Balancing Act 就是求一棵树的重心,然后统计答案. 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define REP(i,n) for(int i(0); i < (n); ++i) 6 #define for_edge(i,x) for(int i = H[x]; i; i = X[i]) 7 8 const int INF = 1 << 30; 9 const int N = 10

poj1655 Balancing Act (dp? dfs?)

Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14247   Accepted: 6026 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

poj1655 Balancing Act(树形dp)

Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11176   Accepted: 4702 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

POJ1655——Balancing Act

Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9143   Accepted: 3797 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

poj1655 Balancing Act(找树的重心)

Balancing Act POJ - 1655 题意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的. /* 找树的重心可以用树形dp或者dfs,这里我用的dfs 基本方法就是随便设定一个根节点,然后找出这个节点的子树大小(包括这个节点),然后总点数减去子树的大小就是向父亲节点走去的点数,使这几部分的最大值最小 */ #include<iostream> #include<cstdio> #include<alg

[POJ1655]Balancing Act

题目描述 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 cr

poj1655 Balancing Act求树的重心

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