The Suspects
Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 35918   Accepted: 17458

Description

Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others. 
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP). 
Once a member in a group is a suspect, all members in the group are suspects. 
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.

Input

The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space. 
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.

Output

For each case, output the number of suspects in one line.

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 代码:
 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define pi acos(-1.0)
#define mj
#define inf 2e8+500
typedef long long ll;
using namespace std;
const int N=3e4+;
int p[N];
int find(int x)
{
return x==p[x]?x:p[x]=find(p[x]);
}
void unit(int x,int y)
{
x=find(x),y=find(y);
if(x==y) return ;
else p[x]=y;
}
int main()
{
int n,m,x,y;
while(scanf("%d%d",&n,&m)==,n||m){
for(int i=;i<n;i++){
p[i]=i;
}
int num;
for(int i=;i<m;i++){
scanf("%d%d",&num,&x);
num--;
while(num--){
scanf("%d",&y);
unit(x,y);
}
}
int ans=;
for(int i=;i<n;i++){
if(find()==find(i)) ans++;
}
printf("%d\n",ans);
}
return ;
}
 

poj 1611 dsu的更多相关文章

  1. poj 1611(并查集)

    http://poj.org/problem?id=1611 题意:有个学生感染病毒了,只要是和这个学生接触过的人都会感染,而和这些被感染者接触的人,也会被感染,现在给定你一些协会的人数,以及所在学生 ...

  2. poj 1611 The Suspects 解题报告

    题目链接:http://poj.org/problem?id=1611 题意:给定n个人和m个群,接下来是m行,每行给出该群内的人数以及这些人所对应的编号.需要统计出跟编号0的人有直接或间接关系的人数 ...

  3. poj 1611 The Suspects(简单并查集)

    题目:http://poj.org/problem?id=1611 0号是病原,求多少人有可能感染 #include<stdio.h> #include<string.h> # ...

  4. 【原创】poj ----- 1611 The Suspects 解题报告

    题目地址: http://poj.org/problem?id=1611 题目内容: The Suspects Time Limit: 1000MS   Memory Limit: 20000K To ...

  5. (并查集)The Suspects --POJ --1611

    链接: http://poj.org/problem?id=1611 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...

  6. POJ - 1611 The Suspects 【并查集】

    题目链接 http://poj.org/problem?id=1611 题意 给出 n, m 有n个人 编号为 0 - n - 1 有m组人 他们之间是有关系的 编号为 0 的人是 有嫌疑的 然后和 ...

  7. 【裸的并查集】POJ 1611 The Suspects

    http://poj.org/problem?id=1611 [Accepted] #include<iostream> #include<cstdio> #include&l ...

  8. 并查集 (poj 1611 The Suspects)

    原题链接:http://poj.org/problem?id=1611 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...

  9. [并查集] POJ 1611 The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 35206   Accepted: 17097 De ...

随机推荐

  1. 微信公众平台开发——helloworld

    威信公众平台有两种模式:编辑模式 和 开发模式. 普通的功能可以通过编辑模式来搞定.开发模式具有更多的功能.让我们来使用开发模式开发helloword吧 步骤如下: 1.先注册一个公众号(https: ...

  2. form中的action与<url-pattern>的理解

    一.<form action="Test/Login" method="post"> 在action中有两种表示方式: 1."/Test/ ...

  3. django orm 多对多自定义第三张表

    # -*- coding: utf-8 -*-# Generated by Django 1.11.11 on 2018-09-02 08:07from __future__ import unico ...

  4. 如何在js中获取到服务器端控件并给其赋值

    如下所示:lbID为服务器端控件ID document.getElementById('<%=lbID.ClientID%>').value = "赋值";

  5. word中已删除设置是否显示

        我们打开别人发过来的文档时经常会出现如下图所示,其实这个不是文档的异常,只是word设置显示了修订版标记,如果我们需要去除这个,只需要在审阅-修订功能下面选择显示最终版即可(测试用的10版,其 ...

  6. 掌握这些技能玩转iOS

    近一年来,苹果iOS/OS X频繁被爆出重大安全漏洞,攻击者可以通过漏洞窃取多达上千个应用的密码.这些漏洞一旦被黑客掌握.利用,后果不堪设想. 好在这些漏洞的发现者还是有节操的,他们都将这些漏洞汇报给 ...

  7. April 8 2017 Week 14 Saturday

    Life is the art of drawing without an eraser. 人生如画,落笔无悔. Yesterday I watched a film from Japan, Afte ...

  8. leetcode: 字符串

    1. palindrome-partitioning Given a string s, partition s such that every substring of the partition ...

  9. 323. Number of Connected Components in an Undirected Graph (leetcode)

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  10. unixbench安装及使用

    unixbench 是一个用于測试 unix 系统性能的工具.也是一个比較通用的 benchmark, 此測试的目的是对类Unix 系统提供一个主要的性能指示,非常多測试用于系统性能的不同方面,这些測 ...