题目描述

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.

输入

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.

 

输出

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

样例输入

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

样例输出

4
1
1
 #include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
int a[];
void cmp()
{
int i;
for(i=;i<;i++)
{
a[i]=i;
}
}
int root(int x)
{
while(a[x]!=x)
x=a[x];
return x;
}
void build(int x,int y)
{
int i,j;
i=root(x);
j=root(y);
if(i!=j)
{
if(i==)
a[j]=i;
else a[i]=j;
}
}
int main()
{
int n,m,k,p,q,i,j,first,next;
while(cin>>n>>m)
{ if(n==&&m==) break;
cmp();
int ii=m;
while(m--)
{
cin>>k>>first;
for(i=;i<k;i++)
{
cin>>next;
build(first,next);
}
}
q=;
if(ii==) cout<<""<<endl;
else
{
for(i=;i<n;i++)
{
if(!root(a[i]))q++;
}
cout<<q<<endl;
}
}
return ;
}

NYNU_省赛选拔题(10)的更多相关文章

  1. NYNU_省赛选拔题(7)

    题目描述 In computer science, a binary tree is a tree data structure in which each node has at most two ...

  2. NYNU_省赛选拔题(5)

    题目描述 P 的一家要出去旅游,买了当地的地图,发现各地分别由各个景点,若 P 想使家人分队去景点,尽快到达各个景点(必须所有景点),并且最终所有家人都到达 M 所在的景点.   你用程序告诉 P 最 ...

  3. NYNU_省赛选拔题(8)

    题目描述 一天萌萌哒孟孟学长去博物馆参观,他想看到更多的东西.博物馆可以表示为N × M细胞的一个矩形区域. “.”表示为路,“*”表示为墙壁,每个墙壁上面都挂有美丽的画卷.孟孟学长可以看到与他所在位 ...

  4. NYNU_省赛选拔题(6)

    题目描述 有一天,小米找到了一个藏宝的迷宫地图,迷宫在一个沙漠里有,迷宫里面有许多宝藏.迷宫里可能有N个藏宝地点,用1到K标记.藏宝地点之间最多有一条通路相连.标记1为迷宫的进出口. 他已经知道其中K ...

  5. NYNU_省赛选拔题(3)

    题目描述 二叉树,若其与自己的镜像完全相同,就称其为镜像树. 是一棵镜像树:而 不是镜像树.现给你一棵二叉树,请你判断其是不是镜像树. 输入 第一行是一个整数数T,表示测试数据有多少组每组数据第一行是 ...

  6. 2013年省赛H题

    2013年省赛H题你不能每次都快速幂算A^x,优化就是预处理,把10^9预处理成10^5和10^4.想法真的是非常巧妙啊N=100000构造两个数组,f1[N],间隔为Af2[1e4]间隔为A^N,中 ...

  7. 2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告

    2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告 勘误1:第6题第4个 if最后一个条件粗心写错了,答案应为1580. 条件应为abs(a[3]-a[7])!=1,宝宝心理苦啊.!感谢zzh ...

  8. HDU-5532//2015ACM/ICPC亚洲区长春站-重现赛-F - Almost Sorted Array/,哈哈,水一把区域赛的题~~

    F - Almost Sorted Array Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  9. 2019ICPC南京网络赛A题 The beautiful values of the palace(三维偏序)

    2019ICPC南京网络赛A题 The beautiful values of the palace https://nanti.jisuanke.com/t/41298 Here is a squa ...

随机推荐

  1. C++ 版本的split_string

    vector<string> split_string(const string &in, char del, bool skip_empty) { vector<strin ...

  2. tomcat 重启进程

    查看端口: ps -aux | grep tomcat 发现并没有8080端口的Tomcat进程. 使用命令:netstat –apn 查看所有的进程和端口使用情况.发现下面的进程列表,其中最后一栏是 ...

  3. OTG驱动分析(二)

    上回介绍了OTG功能的 OTG部分驱动,本片分析OTG功能的从设备部分驱动.从设备的注冊过程和OTG的一样,首先注冊设备. 流程是: 1.定义platform_device结构. 2.定义platfo ...

  4. Java OCR tesseract 图像智能字符识别技术

    公司有需求啊,所以就得研究哈,最近公司需要读验证码,于是就研究起了图像识别,应该就是传说中的(OCR:光学字符识别OCR),下面把今天的收获整理一个给大家做个分享. 本人程序用的tesseract,官 ...

  5. gif动图快速制作方法(附工具)(转)

    现在写博客或是wiki的过程中,会经常引用到图片,特别是客户端经常与页面相关所以截图不可避.但是越来越多的效果仅仅一张图片是无法清楚的描述.并且博客或是wiki也是支持gif图的.gif图的制作方法有 ...

  6. C++输入输出进制、数据宽度与对齐、精度、取整

    cout<<setw(4)<<setfill('0')<<a<<endl; ////样例输出 a=41输出 0041 1.数的进制 [转载]未完的c++ ...

  7. vector和list删除元素

    #include <iostream> #include <vector> #include <list> using namespace std; int mai ...

  8. 汉诺塔问题的java递归实现

    import java.util.Scanner; public class Hanoi { int count=0; public void hanoi(int n,char A,char B,ch ...

  9. 在用TabbarController中出现navigationController 嵌套报错

    假设出现: nested push animation can result in corrupted navigation bar Finishing up a navigation transit ...

  10. linuxIO刷新机制fsync和fdatasync详细解释

    前言: Linux,unix在内核中设有 缓冲区快速缓冲或页面快速缓冲.大多数磁盘I/O都通过缓冲进行,採用延迟写技术. sync:将全部改动过的快缓存区排入写队列.然后返回.并不等待实际写磁盘操作结 ...