Balancing Act
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 8677 | Accepted: 3580 |
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
从某一点出发DFS搜下去,搜到叶子是,把叶子的cnt值赋为1,dp值赋为max(所有孩子节点的cnt值与n-本身的cnt值)
最后遍历一遍找出最小的dp值
记录每一点的num值,就只能从某一点往下搜,不会往上搜(不然会往上搜,因为上一点也相邻)
刚开始用临接矩阵,果断超了内存,用vector就过了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<climits>
#include<cmath>
using namespace std;
const int N=20001;
int number;
vector<int> g[N];
int cnt[N],dp[N],num[N];
bool used[N];
int n;
void dfs(int u)
{
used[u]=1;
num[u]=number++;
int cnt=g[u].size();
for(int i=1;i<=cnt;i++)
{
if(!used[g[u][i-1]])
{
dfs(g[u][i-1]);
}
}
}
void solve(int u)
{
int flag=1;
int cntt=g[u].size();
for(int i=1;i<=cntt;i++)
{
int x=g[u][i-1];
if(num[u]<num[x])
{
flag=0;
solve(x);
cnt[u]+=cnt[x];
}
}
if(flag)
{
cnt[u]=1;
dp[u]=n-1;
return;
}
cnt[u]+=1;
dp[u]=n-cnt[u];
for(int i=1;i<=cntt;i++)
if(num[u]<num[g[u][i-1]])
dp[u]=max(dp[u],cnt[g[u][i-1]]);
}
int main()
{
int tc;
scanf("%d",&tc);
while(tc--)
{
memset(g,0,sizeof(g));
memset(used,0,sizeof(used));
memset(cnt,0,sizeof(cnt));
memset(dp,0,sizeof(dp));
scanf("%d",&n);
for(int i=1;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
g[a].push_back(b);
g[b].push_back(a);
}
number=1;
dfs(1);
solve(1);
int ans=dp[1];
int flag=1;
for(int i=2;i<=n;i++)
{
if(ans>dp[i])
{
ans=dp[i];
flag=i;
}
}
printf("%d %d\n",flag,ans);
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<climits>
#include<cmath>
using namespace std;
const int N=20001;
int number;
vector<int> g[N];
int cnt[N],dp[N],num[N];
bool used[N];
int n;
void dfs(int u)
{
used[u]=1;
num[u]=number++;
int cnt=g[u].size();
for(int i=1;i<=cnt;i++)
{
if(!used[g[u][i-1]])
{
dfs(g[u][i-1]);
}
}
}
void solve(int u)
{
int flag=1;
int cntt=g[u].size();
for(int i=1;i<=cntt;i++)
{
int x=g[u][i-1];
if(num[u]<num[x])
{
flag=0;
solve(x);
cnt[u]+=cnt[x];
}
}
if(flag)
{
cnt[u]=1;
dp[u]=n-1;
return;
}
cnt[u]+=1;
dp[u]=n-cnt[u];
for(int i=1;i<=cntt;i++)
if(num[u]<num[g[u][i-1]])
dp[u]=max(dp[u],cnt[g[u][i-1]]);
}
int main()
{
int tc;
scanf("%d",&tc);
while(tc--)
{
memset(g,0,sizeof(g));
memset(used,0,sizeof(used));
memset(cnt,0,sizeof(cnt));
memset(dp,0,sizeof(dp));
scanf("%d",&n);
for(int i=1;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
g[a].push_back(b);
g[b].push_back(a);
}
number=1;
dfs(1);
solve(1);
int ans=dp[1];
int flag=1;
for(int i=2;i<=n;i++)
{
if(ans>dp[i])
{
ans=dp[i];
flag=i;
}
}
printf("%d %d\n",flag,ans);
}
return 0;
}
1394

被折叠的 条评论
为什么被折叠?



