Pagini recente » Cod sursa (job #978467) | Cod sursa (job #1706260) | Cod sursa (job #2680590) | Cod sursa (job #2518707) | Cod sursa (job #3194174)
#include <fstream>
#include <algorithm>
#include <vector>
#include <bitset>
using namespace std;
ifstream fin ("radiatie.in");
ofstream fout ("radiatie.out");
struct muchie
{
int x,y,c;
};
muchie v[30001];
vector <pair <int,int>> l[15001];
int dif,p,sol,n,m,q,poz,st,dr,x,y,i;
int ant[14][15001],dp[14][15001],t[15001],niv[15001];
bitset <15001> viz;
int cmp (const muchie &a,const muchie &b)
{
return a.c<b.c;
}
int f (int nod)
{
if (t[nod]==nod)
return nod;
return t[nod]=f (t[nod]);
}
void reun (int a,int b)
{
x=f (a);
y=f (b);
t[x]=y;
}
void dfs (int nod,int nivel)
{
viz[nod]=1;
niv[nod]=nivel;
for (int i=0; i<l[nod].size (); i++)
{
int vecin=l[nod][i].first;
int cost=l[nod][i].second;
if (viz[vecin]==0)
{
ant[0][vecin]=nod;
dp[0][vecin]=cost;
dfs (vecin,nivel+1);
}
}
}
int lca (int st,int dr)
{
if (st==dr)
return st;
if (niv[st]<niv[dr])
swap (st,dr);
dif=niv[st]-niv[dr];
for (p=13; p>=0; p--)
{
if ((dif>>p)&1)
st=ant[p][st];
}
for (p=13; p>=0; p--)
{
if (ant[p][st]!=ant[p][dr])
{
st=ant[p][st];
dr=ant[p][dr];
}
}
return st;
}
int f (int x,int dif)
{
if (dif==0)
return 0;
int sol=0;
for (p=13; p>=0; p--)
{
if ((dif>>p)&1)
{
sol=max (sol,dp[p][x]);
x=ant[p][x];
}
}
return sol;
}
int main()
{
fin>>n>>m>>q;
for (i=1; i<=m; i++)
fin>>v[i].x>>v[i].y>>v[i].c;
sort (v+1,v+m+1,cmp);
for (i=1; i<=n; i++)
t[i]=i;
for (i=1; i<=m; i++)
{
if (f (v[i].x)!=f (v[i].y))
{
reun (v[i].x,v[i].y);
l[v[i].x].push_back (make_pair (v[i].y,v[i].c));
l[v[i].y].push_back (make_pair (v[i].x,v[i].c));
}
}
dfs (1,1);
for (p=1; p<=13; p++)
{
for (i=1; i<=n; i++)
{
ant[p][i]=ant[p-1][ant[p-1][i]];
dp[p][i]=max (dp[p-1][i],dp[p-1][ant[p-1][i]]);
}
}
for (i=1; i<=q; i++)
{
fin>>st>>dr;
poz=lca (st,dr);
fout<<max (f (st,niv[st]-niv[poz]),f (dr,niv[dr]-niv[poz]))<<"\n";
}
return 0;
}