Pagini recente » Cod sursa (job #757288) | Cod sursa (job #2360984) | Cod sursa (job #21055) | Cod sursa (job #1045889) | Cod sursa (job #2394109)
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
ifstream f("radiatie.in");
ofstream g("radiatie.out");
vector <int> gp[15005];
struct muchie {int x,y,c;}mu[30005];
int t[15005],h[15005],v[15005],n,m,k,use[15005],i,j,x,y,x1,y1;
int root(int nod)
{
while(t[nod]!=nod) nod=t[nod];
return nod;
}
int cmp(muchie a,muchie b)
{
return a.c<b.c;
}
void df(int nod)
{
int i,vec,sz;
h[nod]++;
use[nod]=1;
sz=gp[nod].size();
for(i=0;i<sz;++i)
{
vec=gp[nod][i];
if(!use[vec])df(vec);
}
}
void unite(int i,int j,int c)
{
int nr;
for(nr=1;nr<=n;nr++) use[nr]=0;
if(h[i]<h[j])
{
t[i]=j;
v[i]=c;
gp[j].push_back(i);
df(i);
}
else
if(h[i]>h[j])
{
t[j]=i;
v[j]=c;
gp[i].push_back(j);
df(j);
}
else
{
h[j]++;
t[j]=i;
v[j]=c;
gp[i].push_back(j);
df(j);
}
}
int query(int x,int y)
{
int ans=0;
while(h[x]>h[y])
{
ans=max(ans,v[x]);
x=t[x];
}
while(h[x]<h[y])
{
ans=max(ans,v[y]);
y=t[y];
}
while(x!=y)
{
ans=max(ans,v[x]);
ans=max(ans,v[y]);
x=t[x];
y=t[y];
}
return ans;
}
int main()
{
f>>n>>m>>k;
for(i=1;i<=m;i++)
f>>mu[i].x>>mu[i].y>>mu[i].c;
sort(mu+1,mu+m+1,cmp);
for(i=1;i<=n;i++) t[i]=i;
i=0;j=1;
while(i<n-1)
{
x1=root(mu[j].x);
y1=root(mu[j].y);
if(x1!=y1)
unite(x1,y1,mu[j].c),i++;
j++;
}
for(i=1;i<=k;i++)
{
f>>x>>y;
g<<query(x,y)<<'\n';
}
return 0;
}