Pagini recente » Cod sursa (job #1292220) | Cod sursa (job #2142391) | Cod sursa (job #222257) | Cod sursa (job #822832) | Cod sursa (job #2389085)
#include <fstream>
#include <vector>
using namespace std;
ifstream in("darb.in");
ofstream out("darb.out");
const int N=100005;
vector<int>a[N];
bool viz[N];
int n,last,lcnt,cnt,d[N];
void dfs(int x)
{
int y;
cnt++;
if(cnt>lcnt)
{
lcnt=cnt;
last=x;
}
viz[x]=true;
for(unsigned i=0;i<a[x].size();i++)
{
y=a[x][i];
if(!viz[y])
{
dfs(y);
d[x]=max(d[x],d[y]+1);
}
}
cnt--;
}
int main()
{
int x,y;
in>>n;
for(int i=1;i<n;i++)
{
in>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
dfs(1);
int max1=0,max2=0;
for(unsigned i=0;i<a[1].size();i++)
{
if(d[a[1][i]]>=max1)
{
max2=max1;
max1=d[a[1][i]];
}
else if(d[a[1][i]]>max2)
max2=d[a[1][i]];
}
int sol=max1+(bool)max1*1+max2+(bool)max2*1+1;
for(int i=1;i<=n;i++)
d[i]=0;
dfs(last);
max1=0;
max2=0;
for(unsigned i=0;i<a[last].size();i++)
{
if(d[a[last][i]]>=max1)
{
max2=max1;
max1=d[a[last][i]];
}
else if(d[a[last][i]]>max2)
max2=d[a[last][i]];
}
sol=max(sol,max1+(bool)max1*1+max2+(bool)max2*1+1);
out<<sol;
return 0;
}