Pagini recente » Cod sursa (job #167672) | Cod sursa (job #2604453) | Cod sursa (job #1831950) | Cod sursa (job #107662) | Cod sursa (job #2541099)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
int n,a[100003],b[100003],maxi,nod;
vector<int>g[100003];
queue<int>q;
void citire()
{
ifstream fin("darb.in");
fin>>n;
int x,y;
for(int i=0;i<n-1;i++)
{
fin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
}
void bfs1()
{
q.push(1);
a[1]=1;
maxi=1;
nod=1;
while(!q.empty())
{
int c=q.front();
q.pop();
for(auto&v:g[c])
if(!a[v])
{
a[v]=a[c]+1;
q.push(v);
if(maxi<a[v])
{
maxi=a[v];
nod=v;
}
}
}
}
void bfs2()
{
q.push(nod);
b[nod]=1;
maxi=1;
while(!q.empty())
{
int c=q.front();
q.pop();
for(auto&v:g[c])
if(!b[v])
{
b[v]=b[c]+1;
q.push(v);
if(maxi<b[v])
maxi=b[v];
}
}
}
void afisare()
{
ofstream fout("darb.out");
fout<<maxi;
}
int main()
{
citire();
bfs1();
bfs2();
afisare();
return 0;
}