Pagini recente » Cod sursa (job #2024617) | Cod sursa (job #586550) | Cod sursa (job #1702787) | Cod sursa (job #1467447) | Cod sursa (job #2154909)
#include <fstream>
#include <queue>
#include <cstring>
#include <vector>
#define MAXN 100005
#define inf 0x3f3f3f3f
using namespace std;
ifstream fin("darb.in");
ofstream fout("darb.out");
int viz[MAXN];
queue <int> Q;
vector <int> graph[MAXN];
inline void BFS(int start) {
int z;
Q.push(start); viz[start] = 1;
while (!Q.empty()) {
z = Q.front();
for (auto x : graph[z]) {
if (viz[x] == 0) {
viz[x] = viz[z] + 1;
Q.push(x);
}
}
Q.pop();
}
}
inline void Read() {
int N, x, y, p;
fin >> N;
for (int i = 1; i < N; i++) {
fin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
BFS(1);
p = 0; viz[p] = 0;
for (int i = 1; i <= N; i++) {
if (viz[i] > viz[p])
p = i;
}
memset(viz, 0, sizeof(viz));
BFS(p);
viz[0] = 0; p = 0;
for (int i = 1; i <= N; i++) {
if (viz[i] > viz[p])
p = i;
}
fout << viz[p];
}
int main () {
Read();
fin.close(); fout.close(); return 0;
}