zoukankan      html  css  js  c++  java
  • poj 1611 简单并查集的应用

    #include<stdio.h>
    #define N 31000
    int pre[N];
    int find(int x) {
    if(x!=pre[x])
        pre[x]=find(pre[x]);
    return pre[x];
    }
    int main() {
      int n,m,i,k,s,t;
      while(scanf("%d%d",&n,&m),n||m){
        for(i=0;i<=n;i++)
            pre[i]=i;
        while(m--) {
            scanf("%d",&s);
            s--;
            scanf("%d",&t);
            while(s--) {
                scanf("%d",&i);
                pre[find(i)]=find(t);
            }
        }
        k=0;
        for(i=0;i<n;i++)
            if(find(i)==find(0))
            k++;
        printf("%d ",k);
      }
    return 0;
    }
  • 相关阅读:
    Linux命令:ssh
    Linux命令:sshpass
    Linux命令:ls
    Linux文件的时间
    Linux命令:findutils
    jfrog
    git
    git branch
    git remote
    java equals 和hashcode
  • 原文地址:https://www.cnblogs.com/thefirstfeeling/p/4410716.html
Copyright © 2011-2022 走看看