并查集&最小生成树、最短路

来源:互联网 发布:郭德纲人品 知乎 编辑:程序博客网 时间:2024/05/21 21:34

并查集

int find(int t)//poj 1213{    if(F[t]==-1) return t;    return F[t]=find(F[t]);}    void bing(int a,int b){    int t1=find(a);    int t2=find(b);    if(t1!=t2) F[t1]=t2;}    main(){    ……    int res=0;        for(int i=1;i<=n;i++)          if(F[i]==-1) res++;        printf("%d\n",res);}

void bing(int x,int y)//poj1856{    int rootx=getfather(x);    int rooty=getfather(y);    if(rootx!=rooty)    {        if(fatherson[rootx]>=fatherson[rooty])  //  这就是要比较了        {            father[rooty]=rootx;            fatherson[rootx]+=fatherson[rooty];        }        else        {            father[rootx]=rooty;            fatherson[rooty]+=fatherson[rootx];        }    }    else      return ;}


0 0
原创粉丝点击