Pagini recente » Cod sursa (job #2260565) | Cod sursa (job #2760230) | Cod sursa (job #24776) | Cod sursa (job #999124) | Cod sursa (job #2693760)
#include<bits/stdc++.h>
using namespace std;
ifstream fin("darb.in");
ofstream fout("darb.out");
int n, x, y, i, nivel_maxim, pozitie_nivel;
bool vizitat[100005];
vector < int > muchii[100005];
void DFS(int nod, int nivel) {
vizitat[nod] = true;
if (nivel > nivel_maxim) {
nivel_maxim = nivel;
pozitie_nivel = nod;
}
for (unsigned int i = 0; i < muchii[nod].size(); i++) {
int vecin = muchii[nod][i];
if (!vizitat[vecin])
DFS(vecin, nivel+1);
}
}
void reset() {
for (i = 1; i <= n; i++)
vizitat[i] = false;
}
int main() {
fin >> n;
for (i = 1; i < n; i++) {
fin >> x >> y;
muchii[x].push_back(y);
muchii[y].push_back(x);
}
DFS(1, 0);
reset();
DFS(pozitie_nivel, 0);
fout << nivel_maxim + 1;
}