zoukankan      html  css  js  c++  java
  • 并查集——poj1611(入门)

    传送门:The Suspects

    • 并查集水题
      #include <iostream>
      #include <cstdio>
      #include <algorithm>
      using namespace std;
      
      const int maxn = 50005;
      int n,m;
      int a[maxn],b,ans;
      int pre[maxn];
      
      void init()
      {
      	for(int i=0;i<n;i++){
      		pre[i] = i;
      	}
      }
      
      int findPre(int x)  
      {  
           if(pre[x]==x)  
              return x;  
          else  
              return pre[x]=findPre(pre[x]);  
      }  
      
      void unite(int x,int y)
      {
      	x = findPre(x);
      	y = findPre(y);
      	if(x==y)	return;
      	pre[y] = x;				//y的上级变为x的祖先节点 			
      }
      
      int main()
      {
      	while(scanf("%d%d",&n,&m) && !(n==0&&m==0)){
      		init();
      		int num,sum=0;
      		for(int i=0;i<m;i++){
      			scanf("%d",&num);
      			if(num>=1){
      				for(int j=0;j<num;j++){
      					scanf("%d",&a[j]);
      				}
      				for(int j=1;j<num;j++){
      					unite(a[0],a[j]);
      				}
      				
      			}
      		}
      		for(int i=0;i<n;i++){
      			if(findPre(i) == pre[0]){
      				sum++;
      			}
      		}
      		printf("%d
      ",sum);
      	}
      	return 0;
      }
      
  • 相关阅读:
    超参数调优
    集成学习(一)
    L1范数与L2范数
    HMM与CRF
    主题模型LDA
    性能评估指标
    java-jdk8下载及安装
    pandas相关性分析
    pandas小技巧
    Window—mysql下载及安装
  • 原文地址:https://www.cnblogs.com/xzxl/p/7338035.html
Copyright © 2011-2022 走看看