Cod sursa(job #323803)

Utilizator Magnuscont cu nume gresit sau fals Magnus Data 13 iunie 2009 16:38:36
Problema Radiatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.51 kb
#include<fstream>

using namespace std;

ifstream fin("radiatie.in");
ofstream fout("radiatie.out");

struct muchie {  long x,y,c;
	      }  a[30002];
long n,m,k,p[15001],e,c[15001],l[15001],t;

void citire()
{    int i;
     fin>>n>>m>>k;
     for(i=1;i<=m;i++)
	 fin>>a[i].x>>a[i].y>>a[i].c;
}

void sort(long x,long y)
{    long i,j,p;
     muchie aux;
     if(x<y)
     {   i=x-1;
	 j=y+1;
	 p=a[(x+y)/2].c;
	 while(i<j)
	 {   do i++; while(a[i].c<p);
	     do j--; while(a[j].c>p);
	     if(i<j)
	     {    aux=a[i];
		  a[i]=a[j];
		  a[j]=aux;
	     }
	 }
	 sort(x,i-1);
	 sort(j+1,y);
     }
}

long cauta(long x)
{    int r;
     for(r=x;r!=p[r];r=p[r]);
     return r;
}

void kruskal()
{    long n1,n2,i;
     sort(1,m);
     for(i=1;i<=m;i++)
     {   n1=cauta(a[i].x);
	 n2=cauta(a[i].y);
	 if(n1!=n2)
	 {    c[n1]=a[i].c;
	      e++;
	      p[n1]=n2;
	 }
     }
}

long max(long a, long b)
{    if(a>b) return a;
     return b;
}

int main()
{    int i,j,x,y;
     citire();
     for(i=1;i<=n;i++)
	 p[i]=i;
     kruskal();
     for(i=1;i<=n;i++)
     {   j=i;
	 while(p[j]!=j)
	 {   l[i]++;
	     j=p[j];
	 }
     }
     for(i=1;i<=k;i++)
     {   fin>>x>>y;
	 t=0;
	 while(l[x]>l[y])
	 {   t=max(t,c[x]);
	     x=p[x];
	 }
	 while(l[y]>l[x])
	 {   t=max(t,c[y]);
	     y=p[y];
	 }
	 while(x!=y)
	 {   t=max(t,max(c[x],c[y]));
	     x=p[x];
	     y=p[y];
	 }
	 fout<<t<<"\n";
     }
     fin.close();
     fout.close();
     return 0;
}