Pagini recente » Cod sursa (job #2550669) | Cod sursa (job #2267827) | Cod sursa (job #1910323) | Cod sursa (job #1958920) | Cod sursa (job #1361323)
#include <bits/stdc++.h>
using namespace std ;
const int NMAX = 15005 ;
const int INF = 0x3f3f3f3f ;
ifstream fin("radiatie.in") ;
ofstream fout("radiatie.out") ;
struct edge
{
int a, b, c ;
} aux;
int N, M, K, ind[NMAX], D[NMAX], noduri[NMAX], mas;
bitset <NMAX> use ;
queue <int> Q ;
vector <edge> V;
inline int cmp(edge A, edge B)
{
if(A.c < B.c)
return 1 ;
return 0 ;
}
inline int grupa(int nod)
{
while(ind[nod] != nod)
nod = ind[nod] ;
return nod ;
}
inline void LVL (int nod)
{
if(ind[nod] == nod) return ;
LVL(ind[nod]) ;
noduri[nod] = noduri[ind[nod]] + 1;
}
inline void APM()
{
int x, y ;
int MM = V.size();
for(int i = 0 ; i < MM; ++ i)
{
x = grupa(V[i].a) ;
y = grupa(V[i].b) ;
if(x != y )
{
ind[y] = x ;
D[y] = V[i].c ;
++ mas ;
}
if(mas > N)
break ;
}
for(int i = 1 ; i <= N ; ++ i)
if(noduri[i] == 0)
LVL(i) ;
}
inline void LCA(int x, int y )
{
int val = 0 ;
while(noduri[x] > noduri[y])
{
val = max(val, D[x]) ;
x = ind[x] ;
}
while(noduri[x] < noduri[y])
{
val = max(val, D[y]) ;
y = ind[y] ;
}
while(x != y)
{
val = max(val, max(D[x], D[y])) ;
x = ind[x] ;
y = ind[y] ;
}
fout << val << '\n' ;
}
int main()
{
fin >> N >> M >> K ;
for(int i = 1 ; i <= M ; ++ i)
{
fin >> aux.a >> aux.b >> aux.c ;
V.push_back(aux) ;
}
for(int i = 1 ; i <= N ; ++i)
ind[i] = i ;
sort(V.begin(), V.end(), cmp) ;
APM() ;
while(K --)
{
int x, y ;
fin >> x >> y ;
LCA(x, y);
}
fin.close() ;
fout.close() ;
return 0 ;
}