/ Randle /

记录详情

Runtime Error


  
# 状态 耗时 内存占用
#1 Wrong Answer 23ms 40.055 MiB
#2 Wrong Answer 14ms 41.43 MiB
#3 Wrong Answer 12ms 41.32 MiB
#4 Runtime Error 149ms 61.43 MiB
#5 Runtime Error 172ms 59.426 MiB
#6 Runtime Error 180ms 67.43 MiB
#7 Runtime Error 213ms 63.805 MiB
#8 Runtime Error 216ms 69.43 MiB
#9 Runtime Error 225ms 65.57 MiB
#10 Runtime Error 183ms 63.43 MiB

代码

#include<bits/stdc++.h>
const int maxn=2e6+1;
inline const void read(int &a)
{
	char c=getchar();a=0;
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9')
	{
		a=(a<<1)+(a<<3)+c-'0';
		c=getchar();
	}
}
int n,d=0;
int res,kk=1,point[maxn],next[maxn],father[maxn],to[maxn],ans[maxn],num[maxn],sum[maxn],deep[maxn];
bool vis[maxn];
inline const void add(int a,int b)
{
	next[++d]=point[a];
	point[a]=d;
	to[d]=b;
}
inline const void dfs1(int p)
{
	vis[p]=true;
	int side=point[p];
	num[p]=1;sum[p]+=deep[p];
	while(side)
	{
		if(!vis[to[side]])
		{
			father[to[side]]=p;
			deep[to[side]]=deep[p]+1;
			dfs1(to[side]);
			num[p]+=num[to[side]];sum[p]+=sum[to[side]];
		}
		side=next[side];
	}
}
inline const void dfs2(int p)
{
	//std::cout<<p<<' '<<father[p]<<std::endl;
	if(p!=1)ans[p]=ans[father[p]]-2*num[p]+num[father[p]];
	if(ans[p]>res){kk=p;res=ans[p];}
	vis[p]=true;
	int side=point[p];
	while(side)
	{
		if(!vis[to[side]])dfs2(to[side]);
		side=next[side];
	}
}
int main()
{
	memset(father,0,sizeof(father));
	memset(sum,0,sizeof(sum));
	memset(point,0,sizeof(point));
	memset(ans,0,sizeof(ans));
	read(n);
	int u,v;
	for(int i=1;i<=n-1;i++){read(u);read(v);add(u,v);add(v,u);}
	deep[1]=0;
	memset(vis,false,sizeof(vis));
	dfs1(1);
	memset(vis,false,sizeof(vis));res=ans[1]=sum[1];
	std::cout<<std::endl;
	dfs2(1);
	std::cout<<kk;
	return  0;
}

信息

递交者
类型
递交
题目
树的深度 T1
题目数据
下载
语言
C++
递交时间
2017-10-11 19:33:36
评测时间
2017-10-11 19:33:36
评测机
分数
0
总耗时
1391ms
峰值内存
69.43 MiB