Cod sursa(job #853877)

Utilizator matei_cChristescu Matei matei_c Data 12 ianuarie 2013 14:08:46
Problema Radiatie Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.84 kb
#include<cstdio>
#include<iostream>
#include<vector> 
#include<algorithm> 
using namespace std;

#define maxn 15001
#define maxm 30001

int n, m, k ;

int a[maxm], b[maxm], c[maxm] ;

int tata[maxn] ;
int ind[maxn] ;
int cost[maxn] ;
int dist[maxn] ;

bool cmp(int i1, int i2)
{
    return c[ i1 ] < c[ i2 ] ;
}
 
int unde(int nod)
{
    if( tata[nod] == nod ) 
        return nod ;
	
    return unde( tata[nod] ) ;
}
 
void df(int nod)
{
    if( tata[nod] == nod ) 
    {
        dist[nod] = 1 ;
        return ;
    }
	
    df( tata[nod] ) ;
    dist[nod] = dist[ tata[nod] ] + 1 ;
}
 
void apm()
{
    for(int i = 1; i <= m; ++i ) 
    {
        int x = unde( a[ ind[i] ] ) ;
        int y = unde( b[ ind[i] ] ) ;
		
        if( x == y )
			continue ;
		
        tata[x] = y ;
        cost[x] = c[ ind[i] ] ;
    }
	
    for(int i = 1; i <= n; ++i ) 
        if( dist[i] == 0 ) 
            df(i) ;
}
 
int query (int x, int y)
{
    int sol = 0 ;
	
    if( dist[x] < dist[y] ) 
		swap( x, y );
	
	while( dist[x] > dist[y] )
	{
		sol = max( sol, cost[x] ) ;
		x = tata[x] ;
	}	
    
	while( x != y )
	{
		int maxcost = max( cost[x], cost[y] ) ;
		
		sol = max( sol, maxcost ) ;
		
		x = tata[x] ;
		y = tata[y] ;
	}	
    
	return sol ;	
}
 
int main ()
{
	
    freopen("radiatie.in", "r", stdin);
    freopen("radiatie.out", "w", stdout);
    
	scanf("%d%d%d", &n, &m, &k);
    
	for(int i = 1; i <= m; ++i ) 
    {
        scanf("%d%d%d", &a[i], &b[i], &c[i] );
        ind[i] = i ;
    }
	
    sort( ind + 1, ind + m + 1, cmp ) ;
    
	for(int i = 1; i <= n; ++i ) 
        tata[i] = i ;
    
	apm() ;
    
	for(int i = 1; i <= k; ++i ) 
    {
		int x, y ;
        scanf("%d%d", &x, &y );
        printf("%d\n", query( x, y ) );
    }
	
    return 0 ;
	
}