Balancing Act(poj1655)

Balancing Act

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12703   Accepted: 5403

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题意:给你n个点,n-1条边形成一颗棵树,然后让你找树的重心;思路:树形dp;先dfs求出每个点所形成的子树的权值,然后再dfs求权值中的最大值更新dp。因为当前点的各个子树的权值都知道,那么只要求出当前节点父亲节点的权值,nod-sum[n];复杂度O(n);
 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<string.h>
 6 #include<iostream>
 7 #include<stack>
 8 #include<vector>
 9 using namespace std;
10 typedef long long LL;
11 vector<int>vec[20005];
12 int dp[20005];
13 bool flag[20005];
14 int sum[20005];
15 void dfs(int n);
16 void dfs2(int n);
17 int nod;
18 int main(void)
19 {
20     int t;
21     scanf("%d",&t);
22     while(t--)
23     {
24         int n;
25         scanf("%d",&nod);
26         n = nod;
27         for(int i = 0;i < 20005;i++)
28             vec[i].clear();
29         for(int i = 0; i < n-1; i++)
30         {
31             int a,b;
32             scanf("%d %d",&a,&b);
33             vec[a].push_back(b);
34             vec[b].push_back(a);
35         }
36         memset(flag,0,sizeof(flag));
37         memset(dp,0,sizeof(dp));
38         memset(sum,0,sizeof(sum));
39         dfs(1);
40         memset(flag,0,sizeof(flag));
41         dfs2(1);
42         int id = 0;
43         int maxx = 1e9;
44         for(int i = 1; i <= n; i++)
45         {
46             if(maxx > dp[i])
47                 maxx = dp[i],id = i;
48         }
49         printf("%d %d\n",id,maxx);
50     }
51     return 0;
52 }
53 void dfs(int n)
54 {
55     int i,j;
56     flag[n] = true;
57     for(i = 0; i < vec[n].size(); i++)
58     {
59         int id = vec[n][i];
60         if(!flag[id])
61         {
62             dfs(id);
63             sum[n]+=sum[id];
64         }
65     }
66     sum[n]++;
67 }
68 void dfs2(int n)
69 {
70     flag[n] = true;
71     int i,j;
72     for(i = 0; i < vec[n].size(); i++)
73     {
74         int id = vec[n][i];
75         if(!flag[id])
76         {
77             dp[n] = max(dp[n],sum[id]);
78             dfs2(id);
79         }
80     }
81     dp[n] = max(dp[n],nod-sum[n]);
82 }
时间: 2024-11-20 20:58:04

Balancing Act(poj1655)的相关文章

『Balancing Act 树的重心』

树的重心 我们先来认识一下树的重心. 树的重心也叫树的质心.找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡. 根据树的重心的定义,我们可以通过树形DP来求解树的重心. 设\(Max_i\)代表删去i节点后树中剩下子树中节点最多的一个子树的节点数.由于删去节点i至少将原树分为两部分,所以满足\(\ \frac{1}{2} \leq Max_i\),我们要求的就是一个\(i\),使得\(Max_i\)最小. 对于Max数组,我们可以列出

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

POJ 1655 Balancing Act[树的重心/树形dp]

Balancing Act 时限:1000ms 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