Cod sursa(job #541824)

Utilizator costyv87Vlad Costin costyv87 Data 25 februarie 2011 14:41:56
Problema Lazy Scor 100
Compilator cpp Status done
Runda Romanian Master in Mathematics and Sciences 2011, Ziua 1 Marime 2.37 kb
#include <cstdio>
#include <algorithm>
using namespace std;
FILE *f,*g;
typedef int Huge[18];
int n,m,i;
struct cp{int x,y,z; /*Huge*/ long long  c1,c2;} v[200000];

int TT[200000], RG[200000];

int find(int x)
{
	int R, y;

	//merg in sus pe arbore pana gasesc un nod care pointeaza catre el insusi
	for (R = x; TT[R] != R; R = TT[R]);

	//aplic compresia drumurilor
	for (; TT[x] != x;)
	{
		y = TT[x];
		TT[x] = R;
		x = y;
	}
	return R;
}

void unite(int x, int y)
{
	//unesc multimea cu rang mai mic de cea cu rang mai mare
	if (RG[x] > RG[y])
		TT[y] = x;
			else TT[x] = y;

	//in caz ca rangurile erau egale atunci cresc rangul noii multimi cu 1
	if (RG[x] == RG[y]) RG[y]++;
}



int Sgn(Huge H1, Huge H2) {

  while (H1[0] && !H1[H1[0]]) H1[0]--;
  while (H2[0] && !H2[H2[0]]) H2[0]--;

  if (H1[0] < H2[0]) {
    return -1;
  } else if (H1[0] > H2[0]) {
    return +1;
  }

  for (int i = H1[0]; i > 0; --i) {
    if (H1[i] < H2[i]) {
      return -1;
    } else if (H1[i] > H2[i]) {
      return +1;
    }
  }
  return 0;
}

void intors(Huge A) {
int i,aux;
for (i=1;i<=A[0]/2;i++) {
    aux=A[i];
    A[i]=A[A[0]+i-1];
    A[A[0]+i-1]=aux;
    }
}


/*bool cmp(cp i , cp j) {
int a,b;
a=Sgn(i.c1,j.c1);
if (a!=0) {
    return (a==-1);
    }
else {
    b=Sgn(i.c2,j.c2);
    return (b==1);
    }
}*/

bool cmp2(cp i, cp j) {
if (i.c1!=j.c1) {
    return (i.c1<j.c1);
    }
else
    return (i.c2>j.c2);
}

int main() {
f=fopen("lazy.in","r");
g=fopen("lazy.out","w");
fscanf(f,"%d%d",&n,&m);
for (i = 1; i <= n; i++)
{
TT[i] = i;
RG[i] = 1;
}
char c;
for (i=0;i<m;i++) {
    v[i].z=i+1;
    fscanf(f,"%d%d",&v[i].x,&v[i].y);
    fscanf(f,"%lld%lld",&v[i].c1,&v[i].c2);
    /*fscanf(f,"%c",&c);
    while(c==' ') fscanf(f,"%c",&c);
    while (c>='0' && c<='9') {
        v[i].c1[++v[i].c1[0]]=c-'0';
        fscanf(f,"%c",&c);
        }
    intors(v[i].c1);
    while(c==' ') fscanf(f,"%c",&c);
     while (c>='0' && c<='9') {
        v[i].c2[++v[i].c2[0]]=c-'0';
        fscanf(f,"%c",&c);
        }
    intors(v[i].c2); */

    }
sort(v,v+m,cmp2);
i=0;
int con=0,cc=0,r;
while (i<=m && con<n-1) {
    if (find(v[i].x) != find(v[i].y)) {
        unite(find(v[i].x), find(v[i].y));
        con++;
        fprintf(g,"%d\n",v[i].z);
        }
    i++;
    }


fclose(g);
return 0;
}