Cod sursa(job #383737)

Utilizator ilincaSorescu Ilinca ilinca Data 17 ianuarie 2010 21:48:15
Problema Radiatie Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <stdio.h>
#include <vector> 
#include <algorithm> 
#define nmax 15005
#define mmax 30005
#define pmax 15

using namespace std;

int n, m, k, a [mmax], b [mmax], c [mmax], ind [mmax], t [nmax], cost [nmax], d [nmax];

bool comp (int a, int b)
{
	return c [a]<c [b];
}

int grup (int nod)
{
	if (t [nod] == nod) 
		return nod;
	return grup (t [nod]);
}

void dfs (int nod)
{
	if (t [nod] == nod) 
	{
		d [nod] = 1;
		return;
	}
	dfs (t [nod]);
	d [nod] = d [t [nod]]+1;
}

void apm ()
{
	int i, x, y, aux;
	for (i=1; i <= m; ++i) 
	{
		x=grup (a [ind [i]]);
		y=grup (b [ind [i]]);
		if (x == y)  continue;
		t [x]=y;
		cost [x]=c [ind [i]];
	}
	for (i=1; i <= n; ++i) 
		if (!d [i]) 
			dfs (i);
}

inline int max (int x, int y)
{
	return (x > y)? x:y;
}

int query (int x, int y)
{
	int r=0, aux;
	if (d [x] < d [y]) 
		aux=x, x=y, y=aux;
	for (; d [x] > d [y] ; r = max (r, cost [x]), x = t [x]);
	for (; x != y; r = max (r, max (cost [x], cost [y])), x=t [x], y=t [y]);
	return r;	
}

int main ()
{
	freopen ("radiatie.in", "r", stdin);
	freopen ("radiatie.out", "w", stdout);
	int i, x, y;
	scanf ("%d%d%d", &n, &m, &k);
	for (i=1; i <= m; ++i) 
		scanf ("%d%d%d", &a [i], &b [i], &c [i]);
	for (i=1; i <= m; ++i) 
		ind [i]=i;
	sort (ind+1, ind+1+i, comp);
	for (i=1; i <= n; ++i) 
		t [i]=i;
	apm ();
	for (i=1; i <= k; ++i) 
	{
		scanf ("%d%d", &x, &y);
		printf ("%d\n", query (x, y));
	}
	return 0;
}