Pagini recente » Cod sursa (job #3037474) | Cod sursa (job #607397) | Cod sursa (job #279356) | Cod sursa (job #1100278) | Cod sursa (job #1101410)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <memory.h>
#define newn G[nod][i]
using namespace std;
ifstream fin("darb.in");
ofstream fout("darb.out");
const int Nmax = 1e5 + 100;
int N, DIST[Nmax], last = 1;
vector <int> G[Nmax];
vector <bool> viz(Nmax);
queue <int> Q;
void DFS(int s)
{
viz.assign(N + 1, 0);
memset(DIST, 0, sizeof DIST);
Q.push(s); DIST[s] = 1; viz[s] = 1;
while(!Q.empty())
{
int nod = Q.front(); Q.pop();
last = nod;
for(unsigned i = 0; i < G[nod].size(); i++)
if(!viz[newn])
{
viz[newn] = 1;
DIST[newn] = DIST[nod] + 1;
Q.push(newn);
}
}
}
int main()
{
fin >> N;
for(int i = 1; i < N; i++)
{
int x, y;
fin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
DFS(last);
DFS(last);
fout << DIST[last];
return 0;
}