Pagini recente » Cod sursa (job #585745) | Cod sursa (job #2713914) | Cod sursa (job #2145456) | Cod sursa (job #2680841) | Cod sursa (job #2454917)
#include <fstream>
using namespace std;
ifstream in("darb.in");
ofstream out("darb.out");
int tata[100001],nivel[100001],tatasuprem[100001];
int main()
{
int n,i,max1=0,max2=0,a,b,ind;
in>>n;
for(i=1; i<=n-1; i++)
{
in>>a>>b;
tata[b]=a;
if(tata[tata[b]]==1)
tatasuprem[b]=tata[b];
else
tatasuprem[b]=tatasuprem[a];
nivel[b]=nivel[a]+1;
}
for(i=1; i<=n; i++)
if(nivel[i]>max1)
{
max1=nivel[i];
ind=i;
}
for(i=1; i<=n; i++)
if(nivel[i]==max1&&tatasuprem[i]!=tatasuprem[ind])
{
out<<max1*2+1;
return 0;
}
else if(nivel[i]<max1&&nivel[i]>max2)
max2=nivel[i];
out<<max1+max2+1;
return 0;
}