Pagini recente » Cod sursa (job #892970) | Cod sursa (job #2544380) | Cod sursa (job #2767090) | Cod sursa (job #1820234) | Cod sursa (job #2554014)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream f("radiatie.in");
ofstream g("radiatie.out");
int n,m,k,tati[20000],rang[20000],nivel[20000],tati2[20000],costuri[20000],boss;
vector<int>lv[20000];
struct muchii
{
int x,y,c;
}v[50000];
bool cmp(muchii a,muchii b)
{
return a.c<b.c;
}
int find(int nod)
{
while(nod!=tati[nod])
nod=tati[nod];
return nod;
}
void Union(int x,int y,int i)
{
if(rang[x]<rang[y])
{
costuri[y]=v[i].c;
costuri[x]=v[i].c;
tati[x]=y;
lv[v[i].y].push_back(v[i].x);
boss=y;
}
if(rang[x]>rang[y])
{
costuri[x]=v[i].c;
costuri[y]=v[i].c;
tati[y]=x;
lv[v[i].x].push_back(v[i].y);
boss=x;
}
if(rang[x]==rang[y])
{
tati[x]=y;
rang[y]++;
costuri[y]=v[i].c;
costuri[x]=v[i].c;
lv[v[i].y].push_back(v[i].x);
boss=y;
}
}
void kruskal()
{
for(int i=1;i<=m;i++)
{
int a=find(v[i].x);
int b=find(v[i].y);
if(a!=b)
Union(a,b,i);
}
}
void dfs(int nod,int tata)
{
nivel[nod]=nivel[tata]+1;
for(size_t j=0;j<lv[nod].size();j++)
{
int vecin=lv[nod][j];
dfs(vecin,nod);
}
}
void lca(int x,int y)
{
int cmax=0;
while(nivel[x]>nivel[y])
{
cmax=max(costuri[x],cmax);
x=tati[x];
}
while(nivel[x]<nivel[y])
{
cmax=max(costuri[y],cmax);
y=tati[y];
}
while(x!=y)
{
cmax=max(cmax,max(costuri[x],costuri[y]));
x=tati[x];
y=tati[y];
}
g<<cmax<<'\n';
}
int main()
{
f>>n>>m>>k;
for(int i=1;i<=m;i++)
f>>v[i].x>>v[i].y>>v[i].c;
sort(v+1,v+m+1,cmp);
for(int i=1;i<=n;i++)
{
tati[i]=i;
rang[i]=1;
}
kruskal();
dfs(boss,0);
for(int i=1;i<=k;i++)
{
int x,y;
f>>x>>y;
lca(x,y);
}
return 0;
}