poj 1611 the suspects

来源:互联网 发布:php 生成xml 编辑:程序博客网 时间:2024/06/08 00:52

这个题目就是说现在已经知道0号是非典型性肺炎的患者,现在给你一些人及关系网就是说这些人只要有一个人得病其余的人也全都得病,现在问一共有多少人得病。其实这个题目说简单很简单,其实你只要按照最普通的并查集做法,然后另加一个数组记录这个团体一共有多少人得病就好了。这个题目和hdu virtual friend这个题目是有很大的相似处的,简单模仿一下就都会写了。

#include<iostream>#include<stdio.h>using namespace std;int father[30000],count[30000];int find_ant(int x){if(x!=father[x])father[x]=find_ant(father[x]);return father[x];}void unin(int x,int y){int x_x,y_y;x_x=find_ant(x);y_y=find_ant(y);if(x_x!=y_y){father[y_y]=x_x;count[x_x]=count[y_y]+count[x_x];}}int main(){int n,m,i,k,j,flag,a,t;while(cin>>n>>m){if(n==0&&m==0)break;for(i=0;i<=n-1;i++){father[i]=i;count[i]=1;}for(i=1;i<=m;i++){scanf("%d",&k);if(k==0)continue;scanf("%d",&flag);for(j=2;j<=k;j++){scanf("%d",&a);unin(flag,a);}}t=find_ant(0);cout<<count[t]<<endl;}return 0;}