Pagini recente » Cod sursa (job #2261929) | Cod sursa (job #1714637) | Cod sursa (job #1113952) | Cod sursa (job #2172087) | Cod sursa (job #2358192)
#include <bits/stdc++.h>
#define nmax 100005
using namespace std;
ofstream fout("darb.out");
ifstream fin("darb.in");
int n,d[nmax],nodmax;
vector <int> L[nmax];
bitset <nmax> viz;
void Citire()
{
int x,y;
fin >> n;
while(fin >> x >> y)
{
L[x].push_back(y);
L[y].push_back(x);
}
}
void BFS(int k)
{
int nod;
fill(d+1,d+n+1,0);
viz.reset();
viz[k] = 1;
d[k] = 1;
queue <int> q;
q.push(k);
while(!q.empty())
{
nod = q.front();
q.pop();
for(auto i : L[nod])
if(viz[i] == 0)
{
viz[i] = 1;
d[i] = max(d[i],d[nod]+1);
if(d[i] > d[nodmax])
nodmax = i;
q.push(i);
}
}
}
int main()
{
Citire();
BFS(1);
BFS(nodmax);
fout << d[nodmax] << "\n";
fin.close();
fout.close();
return 0;
}