首页 > 其他 > 详细

POJ1611 The Suspects

时间:2014-03-19 07:53:04      阅读:373      评论:0      收藏:0      [点我收藏+]
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
const int maxn = 30100;
const int bound = 30001;
int gn, gm;
int f[maxn], cnt[maxn];
vector<int> v;

int getFather(int x) {
    if(x == f[x]) return x;
    else return f[x] = getFather(f[x]);
}

int main()
{
    int k, input;
    while(scanf("%d%d", &gn, &gm) == 2 && (gn || gm)) {
        for(int i = 0; i <= bound; i++) { f[i] = i; cnt[i] = 1; }
        for(int i = 0; i < gm; i++) {
            scanf("%d", &k);
            v.clear();
            for(int j = 0; j < k; j++) {
                scanf("%d", &input);
                v.push_back(input);
            }
            if(k==1) continue;
            else {
                int first = v[0];
                for(int t = 1; t < (int)v.size(); t++) {
                    int parent = getFather(first);
                    int parentx = getFather(v[t]);
                    if(parent == parentx) continue;
                    else {
                        cnt[parent] += cnt[parentx];
                        f[parentx] = parent;
                    }
                }
            }
        }
        int parent = getFather(0);
        printf("%d\n", cnt[parent]);
    }
    return 0;
}

POJ1611 The Suspects,布布扣,bubuko.com

POJ1611 The Suspects

原文:http://blog.csdn.net/achiberx/article/details/21463473

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!