查看代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
const int mod = 1e9+7;
ll qpow(ll a,ll b){ll res=1;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
struct graph
{
int head[maxn],nxt[maxn<<1],to[maxn<<1],w[maxn<<1],sz;
void init(){memset(head,-1,sizeof(head));}
graph(){init();}
void push(int a,int b,int c){nxt[sz]=head[a],to[sz]=b,w[sz]=c,head[a]=sz++;}
int& operator[](const int a){return to[a];}
}g;
struct EDGE
{
int x,y,val;
}edge[maxn];
int pre[maxn],a[maxn];
int fa[maxn][20],dep[maxn];
void dfs(int now,int p)
{
dep[now]=dep[p]+1;
fa[now][0]=p;
for(int i = 1;i <= 19;++i){
fa[now][i]=fa[fa[now][i-1]][i-1];
}
for(int i = g.head[now];~i;i = g.nxt[i]){
dfs(g[i],now);
}
}
int lca(int x,int y)
{
if(dep[x]<dep[y])swap(x,y);
for(int i = 19;i >= 0;--i){
if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
}
if(x==y)return x;
for(int i = 19;i >= 0;--i){
if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
}
return fa[x][0];
}
int Find(int x)
{
return pre[x]==x?x:pre[x]=Find(pre[x]);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("simple.in", "r", stdin);
freopen("simple.out", "w", stdout);
#endif
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
int cnt = n;
for(int i = 1;i <= n;++i)pre[i]=i;
for(int i = 1;i <= m;++i){
scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].val);
}
sort(edge+1,edge+1+m,[](EDGE a,EDGE b){return a.val<b.val;});
for(int i = 1;i <= m;++i){
int x = Find(edge[i].x);
int y = Find(edge[i].y);
if(x!=y){
int tot = ++cnt;
a[tot]=edge[i].val;
pre[tot]=tot;
pre[x]=tot;
pre[y]=tot;
g.push(tot,x,0);
g.push(tot,y,0);
}
}
dfs(cnt,0);
while(k--){
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",a[lca(x,y)]);
}
return 0;
}