(并查集)The Suspects

题目

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

分析与解答

通过改了一个连接函数join,我们的根的num直接储存他所在的树的结点个数,由于题目说0号事已经有了,那我们找到0号的根然后输出跟的num即可

#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 33000
using namespace std;int per[maxn], num[maxn], n, m;
int a[maxn];int find(int x){if(x == per[x])return x;return per[x] = find(per[x]);
}void join(int x, int y){int fx = find(x);int fy = find(y);if(fx != fy){per[fx] = fy;//把fy规定为fx祖宗 num[fy] = num[fx]+num[fy];//fy所在的数的结点的个数(包含他自己) }return ;
}int main (){while(scanf("%d%d", &n, &m)){if(n==0&&m==0) return 0;for(int i = 0; i < n ; ++i){per[i] = i;num[i] = 1;}while(m--){int t;scanf("%d", &t);for(int i = 0; i < t; ++i){scanf("%d", &a[i]);}for(int i = 0 ; i < t - 1; ++i)join(a[i], a[i + 1]);}int  k = find(0);printf("%d\n", num[k]);}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/560716.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java字节流分为_Java文件流可分为字节流和字符流。

【单选题】TCP 和 UDP 协议的相似之处是 ()【单选题】That was the first time I ______ French with a foreigner.【判断题】【判断题】对互联网和搜索引擎的应用是奥巴马连续淘汰希拉里、麦凯恩等竞争对手的法宝之一。【单选题】friend意为“朋友”,请问,“友谊”如何表达?【…

(最小生成树)prim算法

总结一下&#xff1a; 如果选 0为起点&#xff0c;low_cost[i]表示以 0这个起点到i 这个终点的权值&#xff0c;我们找最小的权值的终点i&#xff0c;然后以i为起点&#xff0c;去更新low_cost[]这个数组&#xff0c;如果与i相连的数是j&#xff0c;现在有两种情况&#xff0c…

Java调用虚拟键盘输入法_Android 点击屏幕空白处收起输入法软键盘(手动打开)...

很多时候&#xff0c;我们在使用应用时&#xff0c;会出现输入法软键盘弹出的问题&#xff0c;通常情况下&#xff0c;我们默认会使用户点击返回键或者下一步对软键盘进行隐藏。为了更好的体验&#xff0c;我们可以实现当用户使用完毕软键盘时。点击屏幕空白区域即可实现收起输…

(并查集)Find them, Catch them

题目 The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is…

(并查集)食物链

题目&#xff1a; 动物王国中有三类动物A,B,C&#xff0c;这三类动物的食物链构成了有趣的环形。A吃B&#xff0c; B吃C&#xff0c;C吃A。 现有N个动物&#xff0c;以1&#xff0d;N编号。每个动物都是A,B,C中的一种&#xff0c;但是我们并不知道它到底是哪一种。 有人用两…

多线程销售问题java_Java多线程Runable售票系统实现过程解析

一、无等待&#xff0c;直接出票【虽然解决了不会冲票问题&#xff0c;但显然不符合实际生活】&#xff1a;package com.thread.sale;public class Sale {public static void main(String[] args) {//悟&#xff0c;那么设计爬虫的时候&#xff0c;下载的资源唯一&#xff0c;使…

(并查集)Wireless Network

问题&#xff1a; 地震发生在东南亚。 ACM&#xff08;亚洲合作医疗团队&#xff09;已经与膝上电脑建立了无线网络&#xff0c;但是一次意外的余震袭击&#xff0c;网络中的所有计算机都被打破了。计算机一个接一个地修复&#xff0c;网络逐渐开始工作。由于硬件限制&#xf…

mysql 索引空间大小_查看数据库表中容量大小,表有多少记录,占多少空间以及索引的大小,以及未使用空间...

直接在sqlserver查询窗执行就OK了&#xff0c;也是网上看到的&#xff0c;对日常管理数据库工作者来说也蛮有用的&#xff0c;可以及时的清理冗余的数据&#xff0c;缓解数据库服务器的压力。Create Table #TableSpaceInfo --创建结果存储表(NameInfo NVarchar(50) ,RowsInfo i…

带权并查集-Building Block

题目&#xff1a; John are playing with blocks. There are N blocks (1 < N < 30000) numbered 1…N。Initially, there are N piles, and each pile contains one block. Then John do some operations P times (1 < P < 1000000). There are two kinds of ope…

怎么解决缺少java.doc_java 生成doc帮助文档时出现的问题

正在加载程序包com.csust.czj的源文件...正在构造 Javadoc 信息...标准 Doclet 版本1.7.0_03正在构建所有程序包和类的树...正在生成D:\EclipseProject\TankWar2.7\doc\com\csust\czj\Blood.html...java.lang.IllegalArgumentExceptionat sun.net.www.ParseUtil.decode(ParseUt…

(最短路)Shopping

你刚搬进一套新公寓&#xff0c;有一长串你需要买的东西。不幸的是&#xff0c;买这么多东西需要去很多不同的商店。你想尽量减少购买所需物品所需的驾驶时间。 你的城市被组织成一组由道路连接的十字路口。你的房子和每家商店都在某个十字路口。你的任务是找到从你家出发的最…

java 设计char类型_JAVA中的char类型

1、JAVA中&#xff0c;char占2字节&#xff0c;16位。可在存放汉字2、char赋值char aa; //任意单个字符&#xff0c;加单引号。char a中;//任意单个中文字&#xff0c;加单引号。char a111;//整数。0~65535。十进制、八进制、十六进制均可。输出字符编码表中对应的字符。注&am…

(dijkstra记录路径)find the longest of the shortest

Marica对Mirko很生气&#xff0c;因为他找到了一个新的女朋友&#xff0c;她想报仇。由于她不住在同一个城市&#xff0c;她开始为长途旅行做准备。我们知道每条路从一个城市到另一个城市需要多少分钟。 米尔科在车里无意中听到其中一条路正在维修&#xff0c;路被堵住了&…

友元是c还是java_[C++]【类】友元的三种声明及注意事项

类允许其他类或者函数访问它的非公有成员&#xff0c;但我们需要将访问类非公有成员的其他类或者函数成为它的友元。一、友元函数如果类想要把一个函数作为他的友元&#xff0c;只需要增加一条以 friend 关键字开始的函数声明语句即可。例如&#xff1a;class A{friend void fr…

(SPFA+最短路变形+回路对起点的影响)Arbitrage

套利是利用货币汇率的差异将一种货币的一个单位转换为同一货币的多个单位。例如&#xff0c;假设1美元兑0.5英镑&#xff0c;1英镑兑10.0法国法郎&#xff0c;1法国法郎兑0.21美元。然后&#xff0c;通过兑换货币&#xff0c;一个聪明的交易者可以从1美元开始购买0.5 * 10.0 * …

java 金庸群侠传_《金庸群侠传》MOD发展史(官方版本 转自铁血丹心论坛)

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼三、MOD初期在事件修改器和场景编辑器被制作出来之后&#xff0c;修改档的制作被引向了新的方向。(此后包括事件修改的“修改档”即可称之为“MOD”&#xff0c;但由于修改的量比较小&#xff0c;故以“初期”命名)这一时期的第一个…

(模拟+floyd)Saving James Bond

题目&#xff1a; This time let us consider the situation in the movie “Live and Let Die” in which James Bond, the world’s most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land at the center of a lake filled with…

python从数据库取数据 显示字段名_如何在python中将SQL数据库中的字段名放入列表中...

以下是我目前掌握的代码&#xff1a;from ConfigParser import *import MySQLdbconfiguration ConfigParser()configuration.read(someconfigfile.conf)db MySQLdb.connect(host configuration.get(DATABASE, MYSQL_HOST),user configuration.get(DATABASE, MYSQL_USER),pa…

详解最短路算法模板(dijkstra+floyd+spfa)

1.Floyd_Warshall算法 核心思路&#xff1a;d[i][j] min{d[i][j], d[i][k] d[k][j]} 从i到j有两种路径&#xff0c;经过k点或是不经过k点&#xff0c;所以我们枚举k即可求所有路的最短路。 适用范围&#xff1a;求任意两点间的最短路&#xff0c;可以有负权&#xff0c;可以…

tomcat java垃圾回收_tomcat启动参数配置,内存和垃圾回收

一般情况下&#xff1a;JAVA_OPTS-Xms2048m -Xmx2048m -XX:MaxPermSize512m -XX:UseParallelGC -XX:ParallelGCThreads8 -XX:UseParallelOldGC -Xloggc:../logs/jvm-gc.log -XX:PrintGCDetails -XX:PrintGCTimeStamps适合于实时要求比较高&#xff1a;-Xms2048m -Xmx2048m -Xmn…