Pagini recente » Borderou de evaluare (job #2011131) | Cod sursa (job #258062) | Borderou de evaluare (job #248857) | Borderou de evaluare (job #2247017) | Cod sursa (job #2651603)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin ("radiatie.in");
ofstream fout ("radiatie.out");
struct muchie
{
int a, b, c;
}v[30001];
struct nod
{
int info, cost;
nod * urm;
}*vecin[15001];
nod * prim[15001];
bool viz[15001];
int tt[15001], rg[15001];
bool gasit_global;
int comparare (muchie x, muchie y)
{
return x.c<y.c;
}
int find (int x)
{
int y, r;
y=x;
while(tt[y]!=y)
{
y=tt[y];
}
r=y;
//aplic compresia drumurilor
while(x!=tt[x])
{
y=tt[x];
tt[x]=r;
x=y;
}
return r;
}
void unite (int x, int y)
{
if(rg[x]>rg[y])
{
tt[y]=x;
}
else tt[x]=y;
if(rg[x]==rg[y]) rg[y]++;
}
void DFS (int x, int y, int &maxim, bool &gasit)
{
//if(gasit_global==1) return; //daca deja l-am gasit nu are rost sa pornesc alte DFS-uri
if(x==y)
{
gasit_global=1;
gasit=1;
return;
}
//cout<<"x= "<<x<<"\ny= "<<y<<"\nmaxim= "<<maxim<<"\n";
viz[x]=1;
//ma uit in vecinii lui x cine nu este marcat si aplic DFS pt el pana gasesc y;
nod * t = prim[x];
bool copie=gasit;
while(t!=NULL)
{
if(viz[t->info]==0 && gasit_global!=1)
{
DFS(t->info, y, maxim, copie);
if(copie==1)
{
gasit=1;
copie=0;
if(t->cost>maxim) maxim=t->cost;
}
}
t=t->urm;
}
}
int main()
{
int n, m, k, i, y, a, b, ct;
fin>>n>>m>>k;
for(i=1; i<=m; i++)
{
fin>>v[i].a>>v[i].b>>v[i].c;
}
sort(v+1, v+m+1, comparare);
for(i=1; i<=n; i++)
{
tt[i]=i;
rg[i]=1;
}
ct=0;
i=1;
while(ct<n-1)
{
int raux1=find(v[i].a);
int raux2=find(v[i].b);
if(raux1!=raux2)
{
ct++;
//cout<<v[i].a<<' '<<v[i].b<<"\n";
//il adaug pe v[i].b la vecinii lui v[i].a si invers
nod * aux1 = new nod;
aux1->info=v[i].b;
aux1->cost=v[i].c;
aux1->urm=NULL;
if(prim[ v[i].a ]==NULL)
{
prim[ v[i].a ]=aux1;
vecin[ v[i].a ]=aux1;
}
else
{
vecin[ v[i].a ]->urm=aux1;
vecin[ v[i].a ]=aux1;
}
nod * aux2 = new nod;
aux2->info=v[i].a;
aux2->cost=v[i].c;
aux2->urm=NULL;
if(prim[ v[i].b ]==NULL)
{
prim[ v[i].b ]=aux2;
vecin[ v[i].b ]=aux2;
}
else
{
vecin[ v[i].b ]->urm=aux2;
vecin[ v[i].b ]=aux2;
}
unite(raux1, raux2);
}
i++;
}
//cout<<"\n";
int maxim;
bool gasit;
for(y=1; y<=k; y++)
{
for(i=1; i<=n; i++)
{
viz[i]=0;
}
fin>>a>>b;
maxim=0;
gasit=0;
gasit_global=0;
DFS(a, b, maxim, gasit);
fout<<maxim<<"\n";
}
}