0分求调
  • 板块B4016 树的直径
  • 楼主echo8
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/12/13 16:55
  • 上次更新2024/12/13 21:25:11
查看原帖
0分求调
1490518
echo8楼主2024/12/13 16:55
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
ll read()
{
   int w=1,s=0;
   char ch=getchar();
   while(ch<'0'||ch>'9')
   {
		 if (ch=='-') w=-1;
		 ch=getchar();
   }
   while (ch>='0'&&ch<='9')
   {
		 s=(s<<1)+(s<<3)+(ch^48);
		 ch=getchar();
   }
   return s*w;
}
const int maxn=1e6+555;
int n;
int h[maxn],cnt=0,ans=-0x3f3f3f3f,vis[maxn],dp[maxn];
struct node
{
	int v,next;
}edge[maxn];
void add(int u,int v)
{
	++cnt;
	edge[cnt].v=v;
	edge[cnt].next=h[u];
	h[u]=cnt;
}
void d(int x)
{
	vis[x]=1;
	for (int i=h[x];i;i=edge[i].next)
	{
		int y=edge[i].v;
		if (vis[y]) continue;
		d(y);
		ans=max(ans,dp[x]+dp[y]+1);
		dp[x]=max(dp[x],dp[y]+1);
	}	
}
signed main() {
	n=read();
	for (int i=1;i<=n-1;i++)
	{
		int u,v;
		u=read(),v=read();
		add(u,v);
	}
	d(1);
	cout<<ans;
	return 0;
}
/*
1 3
3 5
5 6
4 5
2 5
6 9
4 7
5 8
*/

2024/12/13 16:55
加载中...