感染者(并查集入门)

来源:互联网 发布:软件项目计划书医疗 编辑:程序博客网 时间:2024/05/16 10:22
#include <iostream>#include <cstdio>#include <cstring>#define maxsize 30010using namespace std;typedef struct tree1{    int pre;    int num;}Tree;Tree tree[maxsize];int find(int x){    if(tree[x].pre == x){        return x;    }    return (tree[x].pre = find(tree[x].pre));}void merge(int x, int y){    int f1 = find(x);    int f2 = find(y);    if(f1!=f2){        tree[f1].num += tree[f2].num;        tree[f2].pre = f1;    }}int main(){    int m, n, i, j, k;    while(scanf("%d%d",&n, &m)!=EOF){        if(0==n&&0==m){            break;        }        for(i = 0; i < n; i++){            tree[i].pre = i;            tree[i].num = 1;        }        while(m--){            scanf("%d", &k);            int a, b;            scanf("%d", &a);            for(i = 1; i < k; i++){                scanf("%d", &b);                merge(a,b);            }        }        int t  = find(0);        printf("%d\n", tree[t].num);    }    return 0;}/*100 42 1 25 10 13 11 12 142 0 12 99 2200 21 55 1 2 3 4 51 00 0*/http://acm.hrbeu.edu.cn/index.php?act=problem&id=1003&cid=50

原创粉丝点击