Pagini recente » Cod sursa (job #3185090) | Cod sursa (job #1660662) | Cod sursa (job #2919319) | Cod sursa (job #2452347) | Cod sursa (job #2811839)
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("darb.in");
ofstream fout("darb.out");
vector<int> G[100005];
int n, cnt[100005], d, ult;
bool v[100005];
void bfs(int nod) {
queue<int> Q;
for(int i = 1; i <= n; i++) {
cnt[i] = 0;
v[i] = false;
}
cnt[nod] = 1;
v[nod] = true;
Q.push(nod);
while(!Q.empty()) {
nod = Q.front();
Q.pop();
for(auto it : G[nod]) {
if(!v[it]) {
cnt[it] = cnt[nod] + 1;
Q.push(it);
v[it] = true;
d = cnt[it];
ult = it;
}
}
}
}
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);
}
bfs(1);
bfs(ult);
fout << d;
return 0;
}