Pagini recente » Cod sursa (job #628391) | Cod sursa (job #1920178) | Cod sursa (job #2565172) | Cod sursa (job #2883609) | Cod sursa (job #3255108)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f ("darb.in");
ofstream g ("darb.out");
int main()
{
int n,x,y;
f>>n;
int t1[n+1],t2[n+1];
for (int i=0; i<=n; i++) t1[i]=t2[i]=-1;
vector<vector<int>>v(n+1);
while (f>>x,f>>y){
v[x].push_back(y);
v[y].push_back(x);
}
int mp=1;
t1[1]=0;
queue<int>q;
q.push(1);
while (q.size()>0) {
int m=q.front();
q.pop();
for (int i=0; i<v[m].size(); i++){
if (t1[v[m][i]]==-1){
q.push(v[m][i]);
t1[v[m][i]]=t1[m]+1;
}
}
if (t1[m]>t1[mp])mp=m;
}
q.push(mp);
t2[mp]=0;
mp=0;
while (q.size()>0) {
int m=q.front();
q.pop();
for (int i=0; i<v[m].size(); i++){
if (t2[v[m][i]]==-1){
q.push(v[m][i]);
t2[v[m][i]]=t2[m]+1;
}
}
if (t2[m]>t2[mp])mp=m;
}
g<<t2[mp]+1;
return 0;
}