// 'inplace' is okay
void RemoveGapsFromLabelImage(const ImgInt& labels, ImgInt* out)
{
  int width = labels.Width();
  int height = labels.Height();
  out->Reset(width, height);

  std::vector<int> used;
  int x, y;

  // scan image and create vector of all labels found
  for (y=0 ; y<height ; y++)
  {
    for (x=0 ; x<width ; x++)
    {
      int a = labels(x,y);
      if (iFind(used, a) < 0)
      {
        used.push_back(a);
      }
    }
  }

  // scan image, replacing labels
  for (y=0 ; y<height ; y++)
  {
    for (x=0 ; x<width ; x++)
    {
      int a = labels(x,y);
      int newlabel = iFind(used, a);
      assert( newlabel >= 0 );
      (*out)(x,y) = newlabel;
    }
  }
}
Exemplo n.º 2
0
int main()
{
    int i,j,n,e,tx,ty;
    double ans;
    while(scanf("%d",&n) != EOF)
    {
        for(i=0;i<n;i++)
        scanf("%lf%lf",&x[i],&y[i]);
        e=0;
        for(i=0;i<n;i++)
            for(j=i+1;j<n;j++)
            {
                u[e]=i;
                v[e]=j;
                w[e]=iDistance(i,j);
                e++;
            }
        for(i=0;i<n;i++)
            p[i]=i;
        for(i=0;i<e;i++)
            r[i]=i;
        qsort(r,e,sizeof(r[0]),cmp);
        ans=0.0;
        for(i=0;i<e;i++)
        {
            tx=iFind(u[r[i]]);
            ty=iFind(v[r[i]]);
            if(tx!=ty)
            {
                p[tx]=ty;
                ans+=w[r[i]];
            }
        }
        printf("%.2f\n",ans);
    }
    return 0;
}
Exemplo n.º 3
0
int iFind(int x)
{
    return p[x]==x?x:(p[x]=iFind(p[x]));
}