【POJ

来源:互联网 发布:中农数据食堂采配平台 编辑:程序博客网 时间:2024/06/10 07:58

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

题意定义DP[i]为以i为根节点的所有子树中总结点数最大的值,现在我们求dp[i]的最小值,如果有相同序号最小。

一开始用了两个dfs求解,感觉这样思路很清晰。
代码

#include<cstdio>#include<vector>#include<cmath>#include<algorithm>#include<cstring>using namespace std;typedef pair<int,int>pii;#define first fi#define second se#define  LL long long#define fread() freopen("in.txt","r",stdin)#define fwrite() freopen("out.txt","w",stdout)const int MAXN = 2e4+10;const int MAXM = 1e6;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;int n,m;vector<int>ve[MAXN];int dp[MAXN]; int son[MAXN];void init(){    for(int i=0;i<=n;i++)         ve[i].clear();}void dfs(int now,int pre){ //这个dfs只是为了得到以每个节点为根的子树的节点数    son[now]=1;    for(int i=0;i<ve[now].size();i++){        int v=ve[now][i];        if(v==pre) continue;;        dfs(v,now);        son[now]+=son[v];    }}/*对于每个节点(除了根节点)来说,它要不就是以其儿子节点为根的下部子树,要不就是以其父节点为根的上部子树,下部子树的我们第一个dfs已经求过了,上部子树的就是 总节点数n-以自身为根节点的下部子树,遍历一遍取最大值就行了*/void DFS(int now,int pre){    for(int i=0;i<ve[now].size();i++){        int v=ve[now][i];        if(v==pre){             dp[now]=max(dp[now],n-son[now]);             continue;        }        DFS(v,now);        dp[now]=max(dp[now],son[v]);    }} int main(){//  fread();//  fwrite();    int t;scanf("%d",&t);    while(t--){        scanf("%d",&n);m=n-1;        init();        while(m--){            int a,b;scanf("%d%d",&a,&b);            ve[a].push_back(b);            ve[b].push_back(a);        }        dfs(1,-1);    //  for(int i=1;i<=n;i++) printf("%d ",son[i]); puts("");        memset(dp,0,sizeof(dp));         DFS(1,0);    //  for(int i=1;i<=n;i++) printf("%d ",dp[i]);        int ans,sum=inf;        for(int i=1;i<=n;i++){            if(sum>dp[i]) {                sum=dp[i];                ans=i;            }        }        printf("%d %d\n",ans,sum);    }     return 0;}

最后发现其实可以放在同一个dfs中求
代码

#include<cstdio>#include<vector>#include<cmath>#include<algorithm>#include<cstring>using namespace std;typedef pair<int,int>pii;#define first fi#define second se#define  LL long long#define fread() freopen("in.txt","r",stdin)#define fwrite() freopen("out.txt","w",stdout)const int MAXN = 2e4+10;const int MAXM = 1e6;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;int n,m;vector<int>ve[MAXN];int dp[MAXN]; int son[MAXN];void init(){    for(int i=0;i<=n;i++)         ve[i].clear();}void dfs(int now,int pre){//道理是一样的。    son[now]=1;    for(int i=0;i<ve[now].size();i++){        int v=ve[now][i];        if(v==pre) continue;        dfs(v,now);        son[now]+=son[v];        dp[now]=max(dp[now],son[v]);    }    dp[now]=max(dp[now],n-son[now]);}int main(){//  fread();//  fwrite();    int t;scanf("%d",&t);    while(t--){        scanf("%d",&n);m=n-1;        init();        while(m--){            int a,b;scanf("%d%d",&a,&b);            ve[a].push_back(b);            ve[b].push_back(a);        }        memset(dp,0,sizeof(dp));         dfs(1,-1);        int ans,sum=inf;        for(int i=1;i<=n;i++){            if(sum>dp[i]) {                sum=dp[i];                ans=i;            }        }        printf("%d %d\n",ans,sum);    }     return 0;}
原创粉丝点击