Cod sursa(job #387983)

Utilizator ciprianfFarcasanu Alexandru Ciprian ciprianf Data 28 ianuarie 2010 21:57:44
Problema Radiatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.79 kb
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
#define NMAX 15010
#define FOR(i, v) for(vector<int>::iterator i = v.begin(); i != v.end(); ++i)
vector<int> G[NMAX];
int X[2*NMAX], Y[2*NMAX], C[2*NMAX], P[2*NMAX];
int T[NMAX], S[NMAX], RG[NMAX], dep[NMAX];
int n, m, cost, k, sol, ok;
bool compf(int a,int b){
	return C[a] < C[b];
}
int find(int x){
	while(x != T[x]) 
		x = T[x];
	return x;
}
void unite(int x,int y){
	if(RG[x] > RG[y]){
		T[y] = x;
		S[y] = cost;
		G[x].push_back(y);
	}
	else {
		T[x] = y;
		S[x] = cost;
		G[y].push_back(x);
	}
	if(RG[x] == RG[y])
		RG[y]++;
}
/*void df(int x,int y,int &sol){
	while(x != T[x]){
		if(S[x] > sol) sol = S[x];
		if(T[x] == y) {
			ok = 0;
			return;
		}
		x = T[x];
	}
}*/
int max(int x,int y){
	if(x < y) return y;
	return x;
}
void cerinta(int x, int y){
	ok = 1; 
	if(dep[x] < dep[y])
		swap(x, y);
	sol = 0;
	while(dep[x] > dep[y]){
		if(S[x] > sol) sol = S[x];
		x = T[x];
	}
	if(x == y) return;
	while(x != y){
		if(S[x] > sol) sol = S[x];
		if(S[y] > sol) sol = S[y];
		x = T[x];
		y = T[y];
	}
}
void agat(int x,int tata){
	FOR(i, G[x])
		if(*i != tata) {
			dep[*i] = dep[x] + 1;
			agat(*i, x);
		}
}
int main(){
	freopen("radiatie.in", "r", stdin);
	freopen("radiatie.out", "w", stdout);
	scanf("%d%d%d", &n, &m, &k);
	for(int i = 1; i <= n; ++i)
		T[i] = i;
	for(int i = 1; i <= m; ++i){
		scanf("%d%d%d", &X[i], &Y[i], &C[i]);
		P[i] = i;
	}
	sort(P + 1, P + m + 1, compf);
	for(int i = 1; i <= m; ++i)
		if(find(X[P[i]]) != find(Y[P[i]]) ){
			cost = C[P[i]];
			unite(find(X[P[i]]), find(Y[P[i]]));
		}
	agat(find(1), -1);
	for(int i = 1; i <= k; ++i){
		int x, y;
		scanf("%d%d", &x, &y);
		cerinta(x, y);
		printf("%d\n", sol);
	}
	return 0;
}