zoukankan      html  css  js  c++  java
  • 【POJ】1611 The Suspects

    题目链接:http://poj.org/problem?id=1611

    题意:有学生感染了SARS。一个学生可以加入很多小组。n个学生m个小组,每个小组有k个组内成员,后跟着k个成员的组内编号。让你求出有多少学生受到了感染。

    题解:并查集板子题。就是计数那里要注意。

    代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 const int maxn = 30010;
     5 
     6 int f[maxn];
     7 
     8 void init(int n){
     9     for(int i = 0; i < n ;i++){
    10         f[i] = i;
    11     }
    12 }
    13 
    14 int find(int x){
    15     if(x == f[x])
    16         return x;
    17     return f[x] = find(f[x]); 
    18 } 
    19 
    20 void join(int a,int b){
    21     a = find(a);
    22     b = find(b);
    23     if(a != b){
    24         f[a] = b;
    25     }
    26 }
    27 
    28 int a[maxn];
    29 int main(){
    30     int n,m,k;
    31     while(cin>>n>>m){
    32         if(n == 0 && m == 0){
    33             break;
    34         }
    35         init(n);
    36         for(int i = 0; i < m; i++){
    37             cin>>k;
    38             cin>>a[0];
    39             for(int j = 1; j < k ;j++){
    40                 cin>>a[j];
    41                 join(a[0],a[j]);
    42             }
    43         }
    44         int cnt = 0;
    45         for(int i = 0 ;i < n; i++){
    46             if(find(i) == f[0])        //point 
    47                 cnt++;
    48         }
    49         cout<<cnt<<endl;
    50     }
    51     
    52     return 0;
    53 } 
  • 相关阅读:
    Html代码查看器
    Http请求
    HTTP协议
    AsyncTask2
    AsyncTask
    幽灵线程解决方案
    handler消息机制入门
    多叉树的树形背包常见建模方法
    Codeforces Round #263
    怎样在win7下装ubuntu(硬盘版安装)
  • 原文地址:https://www.cnblogs.com/Asumi/p/9747641.html
Copyright © 2011-2022 走看看