Pagini recente » Cod sursa (job #3252217) | Cod sursa (job #1615578) | Cod sursa (job #2199762) | Cod sursa (job #604157) | Cod sursa (job #1931735)
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
const int M = 30100 ;
const int N = 15010 ;
const int LOGN = 15 ;
struct muchie {
int x , y , cost ;
};
muchie muc [ M ];
int tat [ N ];
int noNodes , noEdges , noQuery ;
bool cmp ( muchie a , muchie b ){
return a.cost < b.cost ;
}
int FindTat( int x ){
static int tx , temp ;
tx = x ;
while ( tat [ tx ] >= 0 ){
tx = tat [ tx ];
}
while ( tat [ x ] >= 0 ){
temp = tat [ x ];
tat [ x ] = tx ;
x = temp ;
}
return tx;
}
int MergeTat( int x , int y ){
static int tx ,ty ;
tx = FindTat( x );
ty = FindTat( y );
if ( tx == ty ){
return 0 ;
}
if ( tat [ tx ] < tat [ ty ] ){
tat[ tx ] += tat [ ty ];
tat [ ty ] = tx ;
}else{
tat[ ty ] += tat[ tx ];
tat[ tx ] = ty ;
}
return 1 ;
}
int valAncestor [ N ][ LOGN ];
int lvl [ N ] ;
int nodeAncestor [ N ][ LOGN ];
vector < pair < int , int > > edges[ N ];
void UpdateAncestor ( int x ){
static int i ;
for ( i = 1 ; (1<<i) < lvl [ x ] ; i++){
nodeAncestor [ x ][ i ] = nodeAncestor [ nodeAncestor [ x ][ i - 1 ] ][ i -1 ];
valAncestor [ x ][ i ] = max( valAncestor [ x ] [ i - 1 ] , valAncestor[ nodeAncestor [ x ][ i - 1 ] ][ i -1 ] );
}
}
void dfs( int node ){
vector < pair < int , int > >::iterator it ;
int vec ;
for ( it = edges [ node ].begin() ; it != edges [ node ] .end() ; it++ ){
vec = it->first;
if ( lvl [ vec ] ){
continue ;
}
valAncestor [ vec ][ 0 ] = it -> second ;
lvl [ vec ] = lvl [ node ] + 1;
nodeAncestor [ vec ][ 0 ] = node ;
UpdateAncestor ( vec );
dfs ( vec );
}
}
int vmax ;
int solveQuery( int x , int y ){
static int temp , i ;
vmax = 0 ;
if ( lvl [ x ] < lvl [ y ] ){
temp = x ;
x = y ;
y = temp ;
}
for ( i = LOGN ; i >= 0 ; i -- ){
if ( lvl [ x ] - (1<<i) >= lvl [ y ] ){
vmax = max ( vmax , valAncestor [ x ] [ i ] );
x = nodeAncestor [ x ][ i ];
}
}
if ( x == y ){
return vmax;
}
for ( i = LOGN ; i >= 0 ; i -- ){
if ( lvl[ x ] < (1<<i) ){
continue ;
}
if ( nodeAncestor [ x ][ i ] != nodeAncestor [ y ][ i ] ){
vmax = max ( vmax , valAncestor [ x ][ i ] );
vmax = max ( vmax , valAncestor [ y ][ i ] );
x = nodeAncestor [ x ][ i ] ;
y = nodeAncestor [ y ][ i ] ;
}
}
if ( x == y ){
return vmax;
}
vmax = max ( vmax , valAncestor [ x ] [ 0 ]);
vmax = max ( vmax , valAncestor [ y ] [ 0 ]);
return vmax ;
}
int main(){
int i ;
freopen("radiatie.in","r",stdin);
freopen("radiatie.out","w",stdout);
scanf("%d%d%d",&noNodes , &noEdges , &noQuery );
for ( i = 0 ; i < noEdges ; i++ ){
scanf("%d%d%d",&muc [ i ].x ,&muc [ i ].y ,&muc [ i ].cost );
}
sort ( muc , muc + noEdges , cmp );
memset ( tat , -1 , sizeof tat );
for ( i = 0 ; i < noEdges ; i++ ){
if ( MergeTat( muc[ i ].x , muc[ i ].y ) ){
edges [ muc [ i ].x ].push_back ( make_pair(muc [ i ].y , muc [ i ].cost ) );
}
}
lvl [ 1 ] = 1 ;
dfs ( 1 );
int x ,y ;
for ( i = 0 ; i < noQuery ; i++ ){
scanf("%d%d",&x,&y);
printf("%d\n",solveQuery ( x , y ) );
}
return 0;
}