zoukankan      html  css  js  c++  java
  • poj1611 The suspects【并查集】

    严重急性呼吸系统综合症( SARS), 一种原因不明的非典型性肺炎,从2003年3月中旬开始被认为是全球威胁。为了减少传播给别人的机会, 最好的策略是隔离可能的患者。
    在Not-Spreading-Your-Sickness大学( NSYSU), 有许多学生团体。同一组的学生经常彼此相通,一个学生可以同时加入几个小组。为了防止非典的传播,NSYSU收集了所有学生团体的成员名单。他们的标准操作程序(SOP)如下:
    一旦一组中有一个可能的患者, 组内的所有成员就都是可能的患者。
    然而,他们发现当一个学生被确认为可能的患者后不容易识别所有可能的患者。你的工作是编写一个程序, 发现所有可能的患者。
     
    Input
    输入文件包含多组数据。
    对于每组测试数据:
    第一行为两个整数n和m, 其中n是学生的数量, m是团体的数量。0 < n <= 30000,0 <= m <=500。
    每个学生编号是一个0到n-1之间的整数,一开始只有0号学生被视为可能的患者。
    紧随其后的是团体的成员列表,每组一行。
    每一行有一个整数k,代表成员数量。之后,有k个整数代表这个群体的学生。一行中的所有整数由至少一个空格隔开。
    n = m = 0表示输入结束,不需要处理。
    Output对于每组测试数据, 输出一行可能的患者。 Sample Input
    100 4
    2 1 2
    5 10 13 11 12 14
    2 0 1
    2 99 2
    200 2
    1 5
    5 1 2 3 4 5
    1 0
    0 0
    Sample Output
    4
    1
    1

    输出和0所在集合的元素个数

    合并之和遍历一次就行了


    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    
    const int maxn = 30005;
    int t, n, m;
    int ran[maxn], parent[maxn];
    bool vis[maxn];
    
    void init(int n)
    {
        for(int i = 0; i < n; i++){
            ran[i] = 0;
            parent[i] = i;
            vis[i] = false;
        }
    }
    
    int fin(int x)
    {
        if(x == parent[x]) return x;
    
        int t = parent[x];
        parent[x] = fin(parent[x]);
        return parent[x];
    }
    
    void mer(int x, int y)
    {
        int tx = fin(x);
        int ty = fin(y);
    
        if(tx != ty){
            parent[tx] = ty;
            ran[tx] = ran[ty] + 1;
        }
    
    }
    
    int main()
    {
        while(scanf("%d%d", &n, &m) != EOF && (n != 0 || m != 0)){
            init(n);
            for(int i = 0; i < m; i++){
                int before;
                scanf("%d%d", &t, &before);
                for(int j = 1; j < t; j++){
                    int now;
                    scanf("%d", &now);
                    mer(now, before);
                    before = now;
                }
            }
    
            int cnt = 0;
            for(int i = 0; i < n; i++){
                if(fin(0) == fin(i)){
                    cnt++;
                }
            }
    
            cout<<cnt<<endl;
        }
        return 0;
    }
    


  • 相关阅读:
    Oracle 推出 ODAC for Entity Framework 和 LINQ to Entities Beta版
    Entity Framework Feature CTP 5系列文章
    MonoDroid相关资源
    MSDN杂志上的Windows Phone相关文章
    微软学Android Market推出 Web Windows Phone Marketplace
    使用 Visual Studio Agent 2010 进行负载压力测试的安装指南
    MonoMac 1.0正式发布
    Shawn Wildermuth的《Architecting WP7 》系列文章
    使用.NET Mobile API即51Degrees.mobi检测UserAgent
    MongoDB 客户端 MongoVue
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9643407.html
Copyright © 2011-2022 走看看