Example #1
0
void solve_case()
{
  int i,j,k;

  /* 1. construct edges for all pairs of stones */

  k = 0;
  for (i=0; i<n; i++)
    for (j=i+1; j<n; j++)
      {
	e[k].u = i;
	e[k].v = j;
	e[k].w = SQR(x[i]-x[j])+SQR(y[i]-y[j]);
	k++;
      }

  /* 2. sort edges by length */

  qsort(e,k,sizeof(edge),edgecmp);

  /* 3. generate a forest of one tree per stone */

  for (i=0; i<n; i++)
    make_set(i);

  /* 4. while Freddy and Fiona are not in the same tree, add an edge to the forest */

  for (i=0; !same_set(0,1); i++)
    union_sets(e[i].u,e[i].v);
  
  /* 5. output */

  printf("Scenario #%d\n",++scenario);	
  printf("Frog Distance = %.3f\n\n",sqrt((double)(e[i-1].w)));
}
Example #2
0
        void merge(int x, int y){
            if (!same_set(x, y)){
                int u = find(x); int v = find(y);
                if (rank[u] > rank[v]) {
                    p[v] = u;
		    set_size[u] += set_size[v];
		} else {
                    p[u] = v;
		    set_size[v] += set_size[u];
                    if (rank[u] == rank[v])
                        ++rank[v];
                }
		--disj_sets;
            }
        }
Example #3
0
void union_sets (int x, int y)
{
  if (!same_set(x,y)) link(find_set(x),find_set(y));
}