Pagini recente » Cod sursa (job #962470) | Cod sursa (job #876965) | Cod sursa (job #214247) | Cod sursa (job #1479838) | Cod sursa (job #1368541)
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
ifstream f("darb.in");
ofstream g("darb.out");
int N, x, y, last, D[100005];
vector < int > G[100005];
queue < int > Q;
void bfs(int nod)
{
memset(D, -1, sizeof(D));
D[nod]=1; Q.push(nod);
while (Q.size())
{
int crt=Q.front(); Q.pop();
vector<int>::iterator it=G[crt].begin();
for (; it!=G[crt].end(); ++it)
if (D[*it]==-1)
D[*it]=D[crt]+1, last=*it, Q.push(*it);
}
}
int main()
{
f>>N;
for (int i=1; i<N; ++i)
{
f>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
bfs(1); bfs(last);
g<<D[last]<<'\n';
return 0;
}