void Kruskal(){
    int origen , destino , peso;
    int total = 0;          //Peso total del MST
    int numAristas = 0;     //Numero de Aristas del MST

    MakeSet( V );           //Inicializamos cada componente
    std::sort( arista , arista + E );    //Ordenamos las aristas por su comparador

    for( int i = 0 ; i < E ; ++i ){     //Recorremos las aristas ya ordenadas por peso
        origen = arista[ i ].origen;    //Vértice origen de la arista actual
        destino = arista[ i ].destino;  //Vértice destino de la arista actual
        peso = arista[ i ].peso;        //Peso de la arista actual

        //Verificamos si estan o no en la misma componente conexa
        if( !sameComponent( origen , destino ) ){  //Evito ciclos
            total += peso;              //Incremento el peso total del MST
            MST[ numAristas++ ] = arista[ i ];  //Agrego al MST la arista actual
            Union( origen , destino );  //Union de ambas componentes en una sola
        }
    }

    //Si el MST encontrado no posee todos los vértices mostramos mensaje de error
    //Para saber si contiene o no todos los vértices basta con que el numero
    //de aristas sea igual al numero de vertices - 1
    if( V - 1 != numAristas ){
        puts("No existe MST valido para el grafo ingresado, el grafo debe ser conexo.");
        return;
    }
    puts( "-----El MST encontrado contiene las siguientes aristas-----");
    for( int i = 0 ; i < numAristas ; ++i )
        printf("( %d , %d ) : %d\n" , MST[ i ].origen , MST[ i ].destino , MST[ i ].peso ); //( vertice u , vertice v ) : peso

    printf( "El costo minimo de todas las aristas del MST es : %d\n" , total );
}
// use this constructor to initialize the disjoint set and perform make-set on each element in it
disjointSet::disjointSet(int nV)
{
    DisjointSet = new int[nV + 1];
    len = nV;
    for (int i = 1; i <= nV; i ++)
        MakeSet(i);
}
int main(){
    int t , E , x , y, ans;
    scanf("%d" , &t );

    while( t-- ){
        scanf("%d %d" , &V , &E );
        MakeSet();
        while( E-- ){
            scanf("%d %d" , &x , &y );
            Union( x - 1 , y - 1 );
        }

        //Cuento numero de elementos por conjunto
        memset( numElem , 0 , sizeof( numElem ) );
        for( int i = 0 ; i < V ; ++i ){
            numElem[ Find( i ) ]++;
        }

        //Hallo el maximo
        ans = 0;
        for( int i = 0 ; i < V ; ++i ) ans = ( ans < numElem[ i ] )?numElem[ i ]: ans;
        printf("%d\n" , ans );
    }

    return 0;
}
示例#4
0
void createSets(int ilosc) {
  int i;
  int t[1000];

  for (i = 0; i < ilosc; i++) {
    Z[i] = (t_node*) malloc(sizeof(t_node));
    scanf("%d",&t[i]);
    Z[i] = MakeSet(t[i]);
  }
}
示例#5
0
int main() {
    int wybor;
    char a; 
    //struct node * tab[100];
    //struct krawedz * kraw[100];
    int zbior;
    int zbior1;
    int zbior2;
    int ilosc_krawedzi;
    int drzewo;
    printf("Wybierz funkcję:\n");
    
    while(1) {
        printf("1 : MakeSet\t 2 : Union\t3 : drukuj\t 4 : Kruskal \t0 : Koniec\n");
	scanf("%i", &wybor);
        switch (wybor) {
	    case 1:
		printf("Wybierz zbior :");
		scanf("%i", &zbior);
		printf("Podaj element: ");
		scanf("%s", &a);
		tab[zbior] = MakeSet(a);
		break;
	    case 2:
		printf("Podaj numery indeksow 0-99 dwoch zbiorow oddzielone spacja\nWybierz zbiory: ");
		scanf("%d %d", &zbior1, &zbior2);
		printf("\nPodaj na ktorym indeksie chcesz zapisac polaczony zbior: ");
		scanf("%d", &zbior);
		tab[zbior] = Union(Findset(tab[zbior1]),Findset(tab[zbior2]));
		tab[zbior1] = NULL;
		tab[zbior2] = NULL;
		zbior1=0; //blad z indeksem 0
		zbior2=0;
		break;
	    case 3:
		printf("Wybierz zbior: ");
		scanf("%d", &zbior);
		Drukuj(tab[zbior]);
		break;
	    case 4:
		ilosc_krawedzi = tworzKrawedzie();
		sort(0, ilosc_krawedzi - 1);
		drzewo = Kruskal(ilosc_krawedzi);
		break;
	    case 5:
		podglad(drzewo);
		break;
	    case 0:
		printf("\nKoniec\n");
		return 0;
		break;
	}
    }
}
示例#6
0
int main(){



int n;
printf("\nEnter the number of elements :  ");
scanf("%d",&n);
int *Array=MakeSet(n);

int i;

printf("\nInitially set :\n");

Traverse(Array,n);

printf("\nPress enter to make union\n");
getch();
int ch;
int parent,child;
while(1){
printf("\nEnter your choice \n");
printf("\n1.Union");
printf("\n2.Find");
printf("\n3.Traverse");
printf("\n4.Exit\n");
scanf("%d",&ch);

switch(ch){
case 1:
printf("\nEnter parent , child \n");
scanf("%d %d",&parent,&child);
MakeUnion(Array,parent,child,n);
break;

case 2:
printf("\nEnter an element to find root of : ");
scanf("%d",&child);
printf("\nParent of %d is :  %d\n",child,Array[child]);
break;
case 3:
Traverse(Array,n);
break;

case 4:
return 0;

}
getch();

}


return 0;
}
示例#7
0
文件: kruskal.c 项目: alior/nwp
krawedz *kruskal(krawedz *x, int lk, int lw){
	int i;
	wezel *W[lw+1];
	for(i=1;i<=lw;i++){
		W[i]=MakeSet(i);
	}
	for(i=0;i<lk;i++){
		if(FindSet(W[x[i].w1]) != FindSet(W[x[i].w2]) ){
			/*printf("\n%d.) KRAWEDZ %d-%d o wadze %d \n", i+1, x[i].w1, x[i].w2, x[i].waga);
			printf("    ");
			for(j=1;j<=lw;j++){
				printf("%d ", FindSet(W[j])->head->key);
			}
			printf(" w1:");
			pom=FindSet(W[x[i].w1]);
			for(j=1;pom!=NIL;j++){
				printf("%d->",pom->key);
				pom=pom->next;
			}
			printf(" w2:");
			pom=FindSet(W[x[i].w2]);
			for(j=1;pom!=NIL;j++){
				printf("%d->",pom->key);
				pom=pom->next;
			}
			printf("\n");*/
			x[i].roz=1;
			Union(FindSet((W[x[i].w1])), FindSet((W[x[i].w2])));
			/*printf("    ");
			for(j=1;j<=lw;j++){
				printf("%d ", FindSet(W[j])->head->key);
			}
			printf(" w1:");
			pom=FindSet(W[x[i].w1]);
			for(j=1;pom!=NIL;j++){
				printf("%d->",pom->key);
				pom=pom->next;
			}
			printf(" w2:");
			pom=FindSet(W[x[i].w2]);
			for(j=1;pom!=NIL;j++){
				printf("%d->",pom->key);
				pom=pom->next;
			}
			printf("\n");*/
		}
	}
	return x;
}
示例#8
0
文件: 2485.c 项目: UsuallyGO/POJ
int main()
{
    int n;
    int cases;
    int first, second;
    int counter;
    int dist;

	scanf("%d", &cases);
    do
    {
	scanf("%d", &n);

	for(first=0; first<n; first++)
	    for(second=0; second<n; second++)
		scanf("%d", &Matrix[first][second]);

	counter = 0;
	for(first=0; first<n; first++)
	{
	    for(second=first+1; second<n; second++)
	    {
		if(Matrix[first][second] == 0)
		    continue;
		arr[counter].w = Matrix[first][second];
		arr[counter].u = first;
		arr[counter].v = second;
		arr[counter].sign = 0;
		counter++;
	    }
	}

	MakeSet(n);
	Sort(counter);
//	Show(counter);
        dist = Kruskal(counter);
//	dist = FindMax(counter);
//	ShowSpinTree(counter);
	printf("%d\n\n", dist);
	
	cases--;
    }while(cases > 0);

    return 0;
}
示例#9
0
int main()
{
    struct node* a[MAX];
    struct node* temp;
    int c,x,x1,x2,N,M,i,u,v;
    scanf("%d %d",&N,&M);
    for(i=0;i<N;i++)
        a[i]=MakeSet(i);
    for(i=0;i<M;i++)
    {
        scanf("%d %d",&u,&v);
        if(v<u)
        {
            x2=u;
            u=v;
            v=u;
        }



        if(findSet(a[u])->data!=findSet(a[v])->data)
        {
           // printf("\n%d %d\n",findSet(a[u])->data,findSet(a[v])->data);
            Union(a[u],a[v]);
          //  printf("\nlater %d %d\n",findSet(a[u])->data,findSet(a[v])->data);
            //printf("union\n");
        }

        else{
            printf("NO");
            break;
        }



    }
    if(i==M)
        printf("YES");

   // a[x] = MakeSet(x);



}
示例#10
0
int main() {
    int wybor;
    char a; 
    struct node * tab[100];
    int zbior;
    int zbior1;
    int zbior2;
    printf("Wybierz funkcję:\n");
    
    while(1) {
        printf("1 : MakeSet\t 2 : Union\t3 : drukuj\t0 : Koniec\n");
	scanf("%i", &wybor);
        switch (wybor) {
	    case 1:
		printf("Wybierz zbior :");
		scanf("%i", &zbior);
		printf("Podaj element: ");
		scanf("%s", &a);
		tab[zbior] = MakeSet(a);
		break;
	    case 2:
		printf("Podaj numery indeksow 0-99 dwoch zbiorow oddzielone spacja\nWybierz zbiory: ");
		scanf("%d %d", &zbior1, &zbior2);
		printf("\nPodaj na ktorym indeksie chcesz zapisac polaczony zbior: ");
		scanf("%d", &zbior);
		tab[zbior] = Union(Findset(tab[zbior1]),Findset(tab[zbior2]));
		tab[zbior1] = NULL;
		tab[zbior2] = NULL;
		zbior1=0;
		zbior2=0;
		break;
	    case 3:
		printf("Wybierz zbior: ");
		scanf("%d", &zbior);
		Drukuj(tab[zbior]);
		break;
	    case 0:
		printf("\nKoniec\n");
		return 0;
		break;
	}
    }
}
示例#11
0
int main()
{
    int n,m,i,j,h,a,b,mark;
    h=0;
    while(scanf("%d %d",&n,&m)==2)
    {
    	mark=0;
        if(n==0&&m==0)
            return 0;
        
        
        for(i=1;i<=n;i++)
        {
           MakeSet(i);
        }
		 
		 for(i=0;i<m;i++)
		 {
		 	scanf("%d %d",&a,&b);
		 	Union(a,b);
		 }
		 
        for(i=1;i<=n;i++)
        {
            record[FindSet(i)]++;
        }
    
        for(i=1;i<=n;i++)
        {
            if(record[i]!=0)
            {
                mark++;
            }
       
        }
        h++;
    printf("Case %d: %d\n",h,mark);
	mark=0;
    }
 return 0;
}
示例#12
0
文件: cbc.c 项目: rafaelalmeida/poc
Region *MakeRegions(Graph *g, Image *mask)
{
  Region *r=NULL;
  int L, a, b, h, v, s;
  bool stop;
  Set *set;
  int p, q;
  int i, n;

  set = CreateSet(g->nnodes);
  n   = g->nnodes;
  for (i=0;i<g->nnodes;i++)
    if (mask->val[i])
      MakeSet(set, i);
    else
      n--;

  QuickSort(g->edges, g->nedges, sizeof(Edge), CompareEdge);

  stop = false;
  for (i=0;i<g->nedges && !stop;i++) {
    if (mask->val[g->edges[i].p] && mask->val[g->edges[i].q]) {
      p = FindSet(set,g->edges[i].p);
      q = FindSet(set,g->edges[i].q);
      if (p != q) {
        L = g->nodes[p].L / g->nodes[p].s - g->nodes[q].L / g->nodes[q].s;
        a = g->nodes[p].a / g->nodes[p].s - g->nodes[q].a / g->nodes[q].s;
        b = g->nodes[p].b / g->nodes[p].s - g->nodes[q].b / g->nodes[q].s;
        if (100 * (L * L + a * a + b * b) < MIN_DISTANCE * 195075) {
          L = g->nodes[p].L + g->nodes[q].L;
          a = g->nodes[p].a + g->nodes[q].a;
          b = g->nodes[p].b + g->nodes[q].b;
          h = g->nodes[p].h + g->nodes[q].h;
          v = g->nodes[p].v + g->nodes[q].v;
          s = g->nodes[p].s + g->nodes[q].s;
          Union(set,p,q);
          p = FindSet(set,q);
          g->nodes[p].L = L;
          g->nodes[p].a = a;
          g->nodes[p].b = b;
          g->nodes[p].h = h;
          g->nodes[p].v = v;
          g->nodes[p].s = s;
          n--;
        }
        else
          stop = true;
      }
    }
  }

  for (; i<g->nedges; i++) {
    if (mask->val[g->edges[i].p] && mask->val[g->edges[i].q]) {
      p = FindSet(set,g->edges[i].p);
      q = FindSet(set,g->edges[i].q);
      if (p != q) {
        if (100 * g->nodes[p].s < MIN_AREA * g->nnodes ||
            100 * g->nodes[q].s < MIN_AREA * g->nnodes) {
          L = g->nodes[p].L + g->nodes[q].L;
          a = g->nodes[p].a + g->nodes[q].a;
          b = g->nodes[p].b + g->nodes[q].b;
          h = g->nodes[p].h + g->nodes[q].h;
          v = g->nodes[p].v + g->nodes[q].v;
          s = g->nodes[p].s + g->nodes[q].s;
          Union(set,p,q);
          p = FindSet(set,q);
          g->nodes[p].L = L;
          g->nodes[p].a = a;
          g->nodes[p].b = b;
          g->nodes[p].h = h;
          g->nodes[p].v = v;
          g->nodes[p].s = s;
          n--;
        }
      }
    }
  }

  r = CreateRegion(n);
  i = 0;
  for (p=0; p<set->n; p++)
    if (mask->val[p] && p == FindSet(set,p))
      r->nodes[i++] = g->nodes[p];
  DestroySet(&set);

  return(r);
}
示例#13
0
// @Brief: the constructor which takes the vector, and invoke the MakeSet()
UnionSets :: UnionSets(std :: vector<char> &char_vec){
    head_ = tail_ = nullptr;
    MakeSet(char_vec);
}