Pagini recente » Cod sursa (job #2606581) | Cod sursa (job #962104) | Cod sursa (job #481935) | Cod sursa (job #1934377) | Cod sursa (job #3261354)
#include <fstream>
#include <vector>
#include <queue>
#define NMAX 100002
using namespace std;
ifstream fin("darb.in");
ofstream fout("darb.out");
int N,lmax;
vector<int> graph[NMAX];
void citire()
{
int a,b;
fin>>N;
for(int i=1; i<=N-1; i++)
{
fin>>a>>b;
graph[a].push_back(b);
graph[b].push_back(a);
}
}
int BFS(int start)
{
int vmax=0;
vector<int> cost(NMAX,-1);
queue<int> q;
cost[start]=0;
q.push(start);
while(!q.empty())
{
int nod=q.front();
q.pop();
for(int i=0; i<graph[nod].size(); i++)
{
if(cost[graph[nod][i]]==-1)
{
cost[graph[nod][i]]=cost[nod]+1;
vmax=max(vmax,cost[graph[nod][i]]);
q.push(graph[nod][i]);
}
}
}
return vmax;
}
int main()
{
citire();
lmax=0;
for(int i=1; i<=N; i++)
{
int val=BFS(i);
lmax=max(lmax,val);
}
fout<< lmax+1 << "\n";
return 0;
}