Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

55,我的也是

Posted by ZJ75211 at 2017-02-01 17:44:39 on Problem 3107
In Reply To:神啊!!!!谁能告诉我为什么会Runtime Error Posted by:blackchaoxu at 2007-01-30 12:40:44
#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;
int dp[50005],cnt;
struct edge{
	int v,next;
}e[50005];
int head[50005];
void add(int u,int v)
{
	e[++cnt].v=v;
	e[cnt].next=head[u];
	head[u]=cnt;
}
int n;
int an[50005];
int dfs(int u,int fa)
{
	int ch=1,c;
	for(int i=head[u];i!=0;i=e[i].next)
	{
		int v=e[i].v;
		if(v==fa) continue;
		c=dfs(v,u);
		ch+=c;
		dp[u]=max(dp[u],c);
	}
	dp[u]=max(dp[u],n-ch);
	return ch;
}
int main()
{
	
    scanf("%d",&n);
	int a,b;
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&a,&b);
		add(a,b);
		add(b,a);
	}
	int aa=dfs(1,0);
	int tot=0;
	an[0]=0x3f3f3f;
	for(int i=1;i<=n;i++) 
	{
	if(an[0]==dp[i]) an[++tot]=i;
	if(an[0]>dp[i]) an[0]=dp[i],tot=0,an[++tot]=i;
	
   }
   for(int i=1;i<=tot;i++)
	printf("%d ",an[i]);
	
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator