Pagini recente » Cod sursa (job #1792425) | Cod sursa (job #2107584) | Cod sursa (job #209544) | Cod sursa (job #446540) | Cod sursa (job #1761738)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <cstring>
using namespace std;
vector<int> muc[100003];
vector <int>::iterator it;
stack <int>next;
int use[100003],n;
int main()
{
ifstream fin ("darb.in");
ofstream fout ("darb.out");
fin>>n;
int i=0,x,y;
while(i<n-1)
{
fin>>x>>y;
muc[x].push_back(y);
muc[y].push_back(x);
++i;
}
next.push(1);
int depth=1,depthit=1;
while(!next.empty())
{
x=next.top();
next.pop();
it=muc[x].begin();
while(it!=muc[x].end())
{
if(!use[*it])
{
next.push(*it);
use[*it]=use[x]+1;
if(depth<use[*it])
depth=use[*it],depthit=*it;
}
++it;
}
}
memset(use,0,sizeof(use));
next.push(depthit);
use[depthit]=1;
while(!next.empty())
{
x=next.top();
next.pop();
it=muc[x].begin();
while(it!=muc[x].end())
{
if(!use[*it])
{
next.push(*it);
use[*it]=use[x]+1;
if(depth<use[*it])
depth=use[*it];
}
++it;
}
}
fout<<depth;
return 0;
}