Pagini recente » Cod sursa (job #2076573) | Cod sursa (job #2869859) | Cod sursa (job #1477503) | corona_day1 | Cod sursa (job #3268440)
#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,root,mch,x,y,t[nmax],a[20][3*nmax],sir[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;
root=rx;
}
void apm(){
root=1;
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;
sir[++k]=nod;
poz[nod]=k;
for(auto i:v[nod])
if(!viz[i.first]){
a[0][k]=i.second;
dfs(i.first);
sir[++k]=nod;
a[0][k-1]=i.second;
}
}
void rmq(){
for(int i=1;(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(root);
rmq();
/*for(int j=0;(1<<j)<=k;j++){
for(int i=1;i<=k;i++)
cout<<a[j][i]<<" ";
cout<<'\n';
}*/
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])-1,e=p[dr-st+1];
cout<<max(a[e][st],a[e][dr-(1<<e)+1])<<'\n';
}
return 0;
}