NYNU_省赛选拔题(10)
题目描述
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)的更多相关文章
- NYNU_省赛选拔题(7)
题目描述 In computer science, a binary tree is a tree data structure in which each node has at most two ...
- NYNU_省赛选拔题(5)
题目描述 P 的一家要出去旅游,买了当地的地图,发现各地分别由各个景点,若 P 想使家人分队去景点,尽快到达各个景点(必须所有景点),并且最终所有家人都到达 M 所在的景点. 你用程序告诉 P 最 ...
- NYNU_省赛选拔题(8)
题目描述 一天萌萌哒孟孟学长去博物馆参观,他想看到更多的东西.博物馆可以表示为N × M细胞的一个矩形区域. “.”表示为路,“*”表示为墙壁,每个墙壁上面都挂有美丽的画卷.孟孟学长可以看到与他所在位 ...
- NYNU_省赛选拔题(6)
题目描述 有一天,小米找到了一个藏宝的迷宫地图,迷宫在一个沙漠里有,迷宫里面有许多宝藏.迷宫里可能有N个藏宝地点,用1到K标记.藏宝地点之间最多有一条通路相连.标记1为迷宫的进出口. 他已经知道其中K ...
- NYNU_省赛选拔题(3)
题目描述 二叉树,若其与自己的镜像完全相同,就称其为镜像树. 是一棵镜像树:而 不是镜像树.现给你一棵二叉树,请你判断其是不是镜像树. 输入 第一行是一个整数数T,表示测试数据有多少组每组数据第一行是 ...
- 2013年省赛H题
2013年省赛H题你不能每次都快速幂算A^x,优化就是预处理,把10^9预处理成10^5和10^4.想法真的是非常巧妙啊N=100000构造两个数组,f1[N],间隔为Af2[1e4]间隔为A^N,中 ...
- 2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告
2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告 勘误1:第6题第4个 if最后一个条件粗心写错了,答案应为1580. 条件应为abs(a[3]-a[7])!=1,宝宝心理苦啊.!感谢zzh ...
- HDU-5532//2015ACM/ICPC亚洲区长春站-重现赛-F - Almost Sorted Array/,哈哈,水一把区域赛的题~~
F - Almost Sorted Array Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- 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 ...
随机推荐
- sql二进制数据权限
(3为权限组合值,结果为1=列表 2=新建 4=修改 8=删除) select 3 & 1 select 3 & 2 select 3 & 4 select 3 & 2 ...
- 用Jstack跟踪Cpu占用率的Java线程(转)
以下方法在centOS下执行通过:1.先定位占用cpu高的进程 top 2.使用以下命令 ps p 14766 -L -o pcpu,pid,tid,time,tname,stat,psr | sor ...
- hive RegexSerDe View
EXTERNALkeyword它允许用户创建一个外部表.在表中的同时施工指定的路径中的实际数据(LOCATION).Hive 创建内部表时.会将数据移动到数据仓库指向的路径:若创建外部表,仅记录数据所 ...
- memset功能的具体说明
1.void *memset(void *s,int c,size_t n)总的效果:内存空间开辟了 s 第一 n 字节的值设置为一个值 c. 2.样本#include void main(){cha ...
- HDU 4391 Paint The Wall 段树(水
意甲冠军: 特定n多头排列.m操作 以下是各点的颜色 以下m一种操纵: 1 l r col 染色 2 l r col 问间隔col色点 == 通的操作+区间内最大最小颜色数的优化,感觉非常不科学... ...
- 最短路径:Dijkstra,Bellman,SPFA,Floyd该算法的实施
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzQ4NzA1MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- linuxIO刷新机制fsync和fdatasync详细解释
前言: Linux,unix在内核中设有 缓冲区快速缓冲或页面快速缓冲.大多数磁盘I/O都通过缓冲进行,採用延迟写技术. sync:将全部改动过的快缓存区排入写队列.然后返回.并不等待实际写磁盘操作结 ...
- UVALive 6469 Deranged Exams (排列:力绝对是无辜的高中知识啊)
标题手段 : 给你个n([1,17])表达n无论从数据结构.然后n个对这些术语的定义,让你对这些术语和定义对号入座(相当于进行连线,A术语连A术语的定义).然后一个 k([0,n]).问你至少前k个术 ...
- 【转】linux建立软链接
实例:ln -s /home/gamestat /gamestat linux下的软链接类似于windows下的快捷方式 ln -s a b 中的 a 就是源文件,b是链接文件名,其作用是当进入 ...
- bat(批处理文件)初步 第一篇 基本符号
近期我使用的一款软件中须要大量的环境变量设置,而我又不想讲这些变量都加入到系统的环境变量中,一方面是由于有一些同名的库文件的版本号却不一样,都 写在系统环境中会相互干扰:还有一方面则是大部分的路径仅仅 ...