Pagini recente » Cod sursa (job #1041326) | Cod sursa (job #468872) | Cod sursa (job #2774461) | Cod sursa (job #773704) | Cod sursa (job #1480336)
#include <fstream>
#include <vector>
using namespace std;
ifstream is("darb.in");
ofstream os("darb.out");
#define MaxN 100001
int n, nod_max, dmax;
vector<int> G[MaxN], dist(MaxN);
void Read();
void Dfs(int);
void Reset();
int main()
{
Read();
Dfs(1);
Reset();
Dfs(nod_max);
os << dmax;
is.close();
os.close();
return 0;
}
void Reset()
{
for (int i = 1; i <= n; ++i)
dist[i] = 0;
dist[nod_max] = 1;
dmax = 0;
}
void Dfs(int i)
{
for (const auto& p : G[i])
if (!dist[p])
{
dist[p] = dist[i] + 1;
if (dist[p] > dmax)
{
dmax = dist[p];
nod_max = p;
}
Dfs(p);
}
}
void Read()
{
is >> n;
for (int i = 1, x, y; i < n; ++i)
{
is >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
dist[1] = 1;
}