Cod sursa(job #387946)

Utilizator ciprianfFarcasanu Alexandru Ciprian ciprianf Data 28 ianuarie 2010 19:59:11
Problema Radiatie Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <stdio.h>
#include <algorithm>
using namespace std;
#define NMAX 15010
int X[2*NMAX], Y[2*NMAX], C[2*NMAX], P[2*NMAX];
int T[NMAX], S[NMAX], RG[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;
	}
	else {
		T[x] = y;
		S[x] = cost;
	}
	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(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; 
	int sol1 = 0, sol2 = 0;
	df(x, y, sol1);
	if(ok) {
		df(y, x, sol2);
		if(ok)
			printf("%d\n",max(sol1,sol2));
		else printf("%d\n",sol2);
	}
	else printf("%d\n", sol1);
	
}
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]]));
		}
	for(int i = 1; i <= k; ++i){
		int x, y;
		scanf("%d%d", &x, &y);
		cerinta(x, y);
	}
	return 0;
}