Pagini recente » Cod sursa (job #3259227) | Cod sursa (job #451704) | Cod sursa (job #2317901) | Cod sursa (job #570470) | Cod sursa (job #3207015)
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define chad char
#define mod 666013
#define dim 100005
#define lim 1000000
#define INF 1000000000
#define FOR(i,a,b) for(int i=(a); i<=(b); i++)
#define piii pair<int,pair<int,int> >
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define nr_biti __builtin_popcount
using namespace std;
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
int n,m,k;
struct muchie
{
int x,y,w;
bool operator<(const muchie &other)
{
return w<other.w;
}
}v[dim];
int t[dim];
vector<pii> g[dim];
int root(int nod)
{
if(nod==t[nod])
return nod;
return t[nod]=root(t[nod]);
}
bool unite(int x,int y)
{
x=root(x);
y=root(y);
if(x==y)
return 0;
t[y]=x;
return 1;
}
int c[30][dim];
int st[30][dim];
int h[dim];
void dfs(int nod,int daddy=0)
{
t[nod]=daddy;
st[0][nod]=daddy;
h[nod]=1+h[daddy];
for(auto it:g[nod])
{
if(it.first==daddy)
continue;
c[0][it.first]=it.second;
dfs(it.first,nod);
}
}
void pre()
{
for(int i=1; (1<<i)<=n; i++)
for(int j=1; j<=n; j++)
{
st[i][j]=st[i-1][st[i-1][j]];
c[i][j]=max(c[i-1][j],c[i-1][st[i-1][j]]);
}
}
int bl(int nod,int niv)
{
int z=0;
while(niv)
{
if(niv&1)
nod=st[z][nod];
z++;
niv/=2;
}
return nod;
}
int lca(int x,int y)
{
if(h[x]>h[y])
swap(x,y);
int dif=h[y]-h[x];
y=bl(y,dif);
if(x==y)
return x;
for(int i=20; i>=0; i--)
{
if(st[i][x]!=st[i][y])
{
x=st[i][x];
y=st[i][y];
}
}
return st[0][x];
}
int solve(int nod,int niv)
{
int ans=0;
int z=0;
while(niv)
{
if(niv&1)
{
ans=max(ans,c[z][nod]);
nod=st[z][nod];
}
z++;
niv/=2;
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
fin.tie(nullptr);
fout.tie(nullptr);
fin >> n >> m >> k;
for(int i=1; i<=m; i++)
fin >> v[i].x >> v[i].y >> v[i].w;
sort(v+1,v+1+m);
for(int i=1; i<=n; i++)
t[i]=i;
for(int i=1; i<=m; i++)
{
if(unite(v[i].x,v[i].y))
{
g[v[i].x].pb(mp(v[i].y,v[i].w));
g[v[i].y].pb(mp(v[i].x,v[i].w));
}
}
dfs(1);
pre();
while(k--)
{
int x,y,u;
fin >> x >> y;
u=lca(x,y);
fout << max(solve(x,h[x]-h[u]),solve(y,h[y]-h[u])) << "\n";
}
return 0;
}