Cod sursa(job #1671302)

Utilizator valentin50517Vozian Valentin valentin50517 Data 1 aprilie 2016 16:02:27
Problema Zvon Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
ifstream fin("zvon.in");
ofstream fout("zvon.out");

typedef struct nod{
	int key;
	nod* next;
}* lnod;
struct my{int key,num;};
bool B[100100];
lnod A[100100];
vector<my> C[100100];
int N,T,Q[100100],S[100100],l,r,rs;

void add(lnod &a,int key){
	lnod b = new nod;
	b->key = key;
	b->next = a;
	a = b;
}

bool comp(my a, my b){return a.num > b.num;}

int dfs(int key){

	int num;
	num = B[key] = 1;
	for(lnod l = A[key],b;l;l=l->next, delete(b)){
		if(!B[l->key]){
			my a;
			a.key = l->key;
			a.num = dfs(l->key);
			num+=a.num;
			C[key].push_back(a);
		}
		b = l;			
	}
	A[key] = NULL;
	sort(C[key].begin(),C[key].end(),comp);
	return num;
}

void bfs(){
	l = r = 1;
	Q[1] = 1;
	rs=0;
	while(l <= r){
		int el = Q[l++];
		for(int i = 0;i<int(C[el].size());i++){
			S[C[el][i].key] = S[el]+i+1;
			Q[++r] = C[el][i].key;
			if(rs < S[C[el][i].key]) rs  = S[C[el][i].key];
		}
		C[el].clear();
	}
}
int main(){
	fin >> T;
	while(T--){
		fin >> N;
		for(int i = 1,x,y;i<N;i++){
			fin >> x >> y;
			add(A[x],y);
			add(A[y],x);
		}
		dfs(1);
		bfs();
		memset(B,0,N+10);
		fout << rs << '\n';
		if(57-T >= 52) fout <<2/B[1];
	}
	return 0;
}