Cod sursa(job #1488902)

Utilizator DacianBocea Dacian Dacian Data 20 septembrie 2015 01:46:34
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
int d[100001];
using namespace std;
vector<int> Nout[100001];
int bfs(int n){
	memset(d, -1, sizeof(d));
	queue<int> q;
	q.push(n);
	d[n] = 0;
	int c = n;
	while (!q.empty()){
		int x = q.front();
		q.pop();
		c = x;
		for (auto& i : Nout[x]){
			if (d[i] < 0){
				q.push(i);
				d[i] = d[x] + 1;
			}
		}
	}
	return c;
}
int main(){
	ifstream f("darb.in");
	ofstream of("darb.out");
	int N, a, b;
	f >> N;
	for (int i = 0; i<N; ++i){
		f >> a >> b;
		Nout[a].push_back(b);
		Nout[b].push_back(a);
	}
	int i = bfs(1);
	of << d[bfs(i)] + 1;
	return 0;
}