Pagini recente » Cod sursa (job #639303) | Cod sursa (job #254405) | Cod sursa (job #3259537) | Cod sursa (job #2373786) | Cod sursa (job #2710673)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
ifstream fin("darb.in");
ofstream fout("darb.out");
const int noduriMax = 200000;
vector <int> muchii[noduriMax];
bool used[noduriMax];
int distanta[noduriMax];
int n;
int dist = 0;
void dfs(int nod)
{
dist++;
used[nod] = true;
for (int i = 0; i < muchii[nod].size(); ++i)
{
int vecin = muchii[nod][i];
if (used[vecin] == false)
{
distanta[vecin] = dist;
dfs(vecin);
}
}
dist--;
}
int main()
{
fin >> n;
for (int i = 1; i < n; ++i)
{
int x, y;
fin >> x >> y;
muchii[x].push_back(y);
muchii[y].push_back(x);
}
distanta[1] = 0;
dfs(1);
int maxx_nod;
int maxx_parcurs = -1;
for (int i = 1; i <= n; ++i)
if (maxx_parcurs < distanta[i])
{
maxx_parcurs = distanta[i];
maxx_nod = i;
}
dist = 0;
distanta[maxx_nod] = 0;
for (int i = 1; i <= n; ++i)
used[i] = false;
dfs(maxx_nod);
int maxx = -1;
for (int i = 1; i <= n; ++i)
maxx = max(distanta[i], maxx);
fout << maxx + 1;
}