Pagini recente » Cod sursa (job #1858302) | Cod sursa (job #1777844) | Cod sursa (job #1978869) | Cod sursa (job #940184) | Cod sursa (job #1136066)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <set>
#define DN 15005
#define DM 60005
#define pb push_back
#define mp make_pair
#define per pair<int,int>
#define INF (1<<30)
#define LL long long
#define un unsigned
#define x first
#define y second
using namespace std;
struct muchie{
int a,b,c;
};
muchie M[DN];
vector< per > list[DN];
bitset< DN > viz;
int t[DN],max_val[16][DN],sz,deep[DN],T[16][DN],n,m,k;
bool cmp(muchie A,muchie B){
return A.c<B.c;
}
/// Paduri
int tata(int nod){
while(t[nod]!=nod)
nod = t[nod];
return nod;
}
void update(int nod,int &root){
while(t[nod]!=nod){
int next_nod = t[nod] ;
t[nod] = root;
nod = next_nod;
}
t[ nod ] = root;
}
/// -->
void dfs(int nod,int cst,int nivel){
viz[ nod ] = true;
deep[ nod ] = nivel;
max_val[0][ nod ] = cst;
for(un int i=0;i<list[nod].size();++i){
int next_nod = list[nod][i].x;
int cost = list[nod][i].y;
if(!viz[next_nod]){
T[0][ next_nod ] = nod;
dfs(next_nod,cost,1+nivel);
}
}
}
void build_rmq(){
for(int p=1;p<=15;++p)
for(int i=1;i<=n;++i){
T[p][i] = T[p-1][ T[p-1][i]];
max_val[p][i] = max( max_val[p-1][i] , max_val[p-1][ T[p-1][i] ]);
}
}
int fnd(int a,int b){ /// urc in paralel pana la lca
if(deep[a] < deep[b])
swap(a,b);
int p = 0;
if( a == b )
return 0;
if(deep[a] == deep[b]){
for(;T[p][a] != T[p][b] ; ++p); if(p) --p;
int left = max_val[p][a] , right = max_val[p][b];
return max(left , max( right , fnd(T[p][a],T[p][b]) ));
}
for(;deep[a] - (1<<p) >= deep[b]; ++p); if(p) --p; /// cautam stramosul lui a care e pe acelasi nivel cu bu
return max(max_val[p][a] , fnd( T[p][a] , b) );
}
int main()
{
ifstream f("radiatie.in");
ofstream g("radiatie.out");
f>>n>>m>>k;
for(int i=1;i<=n;++i)
t[i] = i;
for(int i=0;i<m;++i)
f>>M[i].a>>M[i].b>>M[i].c;
sort(M,M+m,cmp);
for(int i=0;i<m;++i){
int t_a = tata(M[i].a);
int t_b = tata(M[i].b);
if(t_a == t_b)
continue;
update(t_b,t_a);
list[ M[i].a ].pb( mp(M[i].b,M[i].c));
list[ M[i].b ].pb( mp(M[i].a,M[i].c));
/// folosim muchia
}
dfs(1,0,1);
build_rmq();
for(;k--;){
int a,b;
f>>a>>b;
g<<fnd(a,b)<<"\n";
}
return 0;
}