Cod sursa(job #415119)

Utilizator GheorgheMihaiMihai Gheorghe GheorgheMihai Data 10 martie 2010 22:23:57
Problema Radiatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include<stdio.h>
#include<algorithm>
using namespace std;
int n,m,q,a[30002],b[30002],c[30002],o[30002],t[15002],h[15002],cost[15002];
inline bool cmp(int i, int j){return c[i]<c[j];}
inline int max(int a, int b){return a>b?a:b;}

int find(int x)
{
	if(x==t[x])
		return x;
	return find(t[x]);
}

void apm()
{
	int i,x,y;
	for(i=1;i<=m;i++)
	{
		x=find(a[o[i]]);
		y=find(b[o[i]]);
		if(x!=y)
		{
			t[x]=y;
			cost[x]=c[o[i]];
		}
	}
}

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

int query(int x, int y)
{
	int z=0;
	if(h[x]<h[y])
		x^=y^=x^=y;
	while(h[x]>h[y])
	{
		z=max(z,cost[x]);
		x=t[x];
	}
	while(x!=y)
	{
		z=max(z,cost[x]);
		z=max(z,cost[y]);
		x=t[x];
		y=t[y];
	}
	return z;
}

int main()
{
	freopen("radiatie.in","r",stdin);
	freopen("radiatie.out","w",stdout);
	scanf("%d%d%d",&n,&m,&q);
	int i;
	for(i=1;i<=m;i++)
		scanf("%d%d%d",&a[i],&b[i],&c[i]),o[i]=i;
	sort(o+1,o+m+1,cmp);
	for(i=1;i<=n;i++)
		t[i]=i;
	apm();
	for(i=1;i<=n;i++)
		if(!h[i])
			dfs(i);
	while(q--)
	{
		scanf("%d%d",&n,&m);
		printf("%d\n",query(n,m));
	}
	return 0;
}