Pagini recente » Cod sursa (job #1320097) | Cod sursa (job #856194) | Cod sursa (job #799410) | Cod sursa (job #1590810) | Cod sursa (job #3268430)
#include <fstream>
#include <algorithm>
#include <vector>
#define nmax 15001
#define mmax 30001
using namespace std;
ifstream cin("radiatie.in");
ofstream cout("radiatie.out");
int n,m,q,k,mch,x,y,t[nmax],a[20][3*nmax],p[3*nmax],poz[nmax];
bool viz[nmax];
vector<pair<int,int>>v[nmax];
struct muchie{
int x,y,c;
}d[mmax];
bool cmp(const muchie& a,const muchie& b){
return a.c<b.c;
}
int get_root(int x){
if(t[x]>0)
return get_root(t[x]);
return x;
}
void join(int x,int y,int c){
int rx=get_root(x),ry=get_root(y);
if(rx==ry)
return ;
mch++;
v[x].push_back({y,c});
v[y].push_back({x,c});
if(t[rx]>t[ry])
swap(rx,ry);
t[rx]+=t[ry];
t[ry]=rx;
}
void apm(){
for(int i=1;i<=m&&mch<n-1;i++)
join(d[i].x,d[i].y,d[i].c);
}
void dfs(int nod){
viz[nod]=1;
a[0][++k]=nod;
poz[nod]=k;
for(auto i:v[nod])
if(!viz[i.first]){
a[1][k]=i.second;
dfs(i.first);
a[0][++k]=nod;
a[1][k-1]=i.second;
}
}
void rmq(){
for(int i=2;(1<<i)<=k;i++)
for(int j=1;j<=k;j++){
a[i][j]=a[i-1][j];
if(j+(1<<(i-1))<=k)
a[i][j]=max(a[i][j],a[i-1][j+(1<<(i-1))]);
}
}
int main()
{
cin>>n>>m>>q;
for(int i=1;i<=m;i++)
cin>>d[i].x>>d[i].y>>d[i].c;
sort(d+1,d+m+1,cmp);
for(int i=1;i<=n;i++)
t[i]=-1;
apm();
dfs(1);
/*for(int i=1;i<=k;i++)
cout<<a[1][i]<<" ";*/
rmq();
for(int i=2;i<=k;i++)
p[i]=p[i/2]+1;
while(q--){
cin>>x>>y;
int st=min(poz[x],poz[y]),dr=max(poz[x],poz[y]),e=p[dr-st+1];
cout<<max(a[e][st],a[e][dr-(1<<e)+1])<<'\n';
}
return 0;
}