Cod sursa(job #318126)

Utilizator AndreiDDiaconeasa Andrei AndreiD Data 26 mai 2009 22:52:51
Problema Radiatie Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.31 kb
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

#define Inf 0x3f3f3f3f

#define file_in "radiatie.in"
#define file_out "radiatie.out"

#define Nmax 15100
#define Mmax 30100

struct muchie{int a,b,c;} v[Mmax];

int n,m,k;
int t[Nmax];
int x,y;
int d[Nmax];
int a[Nmax];
int t1,t2;
int viz[Nmax];
int rez;
int r[Nmax];

int cmp(muchie x,muchie y)      
{      
    return x.c<y.c;      
}      
     
int tata(int x)      
{      
    int y=x,z,aux;      
    while(y!=t[y])      
        y=t[y];      
    z=x;      
    while(z!=t[z])      
    {      
        aux=t[z];      
        t[z]=y;      
        z=aux;      
    }      
    return y;      
} 


void citire()
{
	int i;
	freopen(file_in,"r",stdin);
	freopen(file_out,"w",stdout);
	
	scanf("%d %d %d", &n,&m,&k);
	
	for (i=1;i<=m;++i)
		 scanf("%d %d %d", &v[i].a, &v[i].b, &v[i].c);
}

void apm()
{
	int i;
    for (i=1;i<=n;++i)    
        t[i]=i;      
    memset(r,0,sizeof(0)); 
    sort(v+1,v+m+1,cmp);      
     
    for (i=0;i<m;++i)      
    {      
        t1=tata(v[i].a);    
        t2=tata(v[i].b);      
         
        /*if (t1!=t2)      
        {      
            t[t1]=t2; 
            a[i]=v[i].c;			
        }    */

		if (t1!=t2)
		{   
          if (r[t1]<r[t2]) 
		  { 
			  t[t1]=t2;
			  a[t1]=v[i].c;
		  }   
          else 
		  {
			  t[t2]=t1;
			  a[t2]=v[i].c;
		  }   
          if (r[t1]==r[t2]) 
			  ++r[t1];   
		}
    }      
}

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

void solve()
{
	int i;
	for (i=1;i<=n;++i)   
        if (viz[i]==0)   
            go(i);   
    for (i=1;i<=k;++i)
	{
		scanf("%d %d", &x,&y);
		rez=0;
		int aux;
		if (d[x]<d[y])
		{
			aux=x;
			x=y;
			y=aux;
		}
		while(d[x]>d[y])
		{    
            rez=max(rez,a[x]);   
            x=t[x];   
        }   
		while(x!=y)
		{   
            rez=max(rez,a[x]);   
            x=t[x];   
            rez=max(rez,a[y]);   
            y=t[y];   
        }   
		printf("%d\n", rez);
	}
}

int main()
{
	citire();
	apm();
	solve();
	
	fclose(stdin);
	fclose(stdout);
	
	return 0;
}