`
Simone_chou
  • 浏览: 192851 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

The Suspects(并查集)

    博客分类:
  • POJ
 
阅读更多
The Suspects
Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 18938   Accepted: 9178

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

   题意:

   给出N(0到30000)个同学和M(0到500)个朋友圈,后M行首先为一个T人数,表示这个朋友圈总共有多少人,后面跟随有T个不同的数字,代表这个朋友圈有这些人。求出0同学所在朋友圈的总人数。

 

   思路:

   又是一个判断集合中个数的问题,同样可以用一个rank数组来记录该节点以下包括有多少人。但是这题给出的输入条件与前面做的有点不同,以前做的题都是一行给出两个数,说明这两个数属于同一集合,而这题是一次性给出所有的数属于同一集合,所以输入的时候要有点技巧。属于基础并查集。

AC:

#include<stdio.h>
#define max 30005
int root[max],rank[max];
//查找根节点
int find(int a)
{
	int x=a,t;
	while(x!=root[x])
	  x=root[x];
	while(x!=a)
	{
		t=root[a];
		root[a]=x;
		a=t;
	}
	return x;
}

int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
	{
		for(int i=0;i<=n-1;i++)
		  root[i]=i,rank[i]=1;
		while(m--)
		{
			int t,fir;
			scanf("%d%d",&t,&fir);
//第一次输入分开来,这样子就循环就可以合并这一次和上一次的数了
			t--;
			while(t--)
			{
				int sec;
				int ff,fs;
				scanf("%d",&sec);
				ff=find(fir);
				fs=find(sec);
				if(ff!=fs)  
				{
				 rank[fs]+=rank[ff];
//rank数组登记人数
				 root[ff]=fs;
			    }
				fir=sec;
			}
		}
		printf("%d\n",rank[find(0)]);
//rank[i]是指i下面有多少个结点,那么0所在的集合的父节点的rank即为题意所求
	}
	return 0;
}

   总结:

   与昨天做的并查集没有太大区别,可能区别就在于输入的部分,因为每次给出的数都不是一对一对的,所以要懂得灵活变通。

分享到:
评论

相关推荐

    poj 1611 The Suspects 代码

    poj 1611 The Suspects 代码 并查集的应用

    poj 1611 The Suspects.md

    poj 1611 The Suspects.md

    ACM ZJU 1789 Suspects

    《ACM ZJU 1789 Suspects:并查集在嫌疑犯问题中的应用》 本题目是Zhejiang University(ZJU)在ACM竞赛中的一道题目,编号1789,名为“Suspects”。该题目的核心在于使用并查集(Disjoint Set)数据结构来解决嫌疑...

    phaser-the-usual-suspects:那些你看过一百万次的游戏,现在有了 Phaser

    ##The Usual Suspects 拼图、发现差异、拖拽匹配等,这些游戏真的很简单,但却是你最终一遍又一遍地做的那种游戏,游戏的常见嫌疑人。 这是一组蓝图,您可以下载源代码并制作自己的游戏。 这些蓝图或模板只是作为一...

    A Methodological Model

    focus their activities, geographically prioritize suspects, and to concentrate saturation or directed patrolling efforts in those zones where the criminal predator is most likely to be active. By ...

    Web Security Testing Cookbook: Systematic Techniques to Find Problems Fast

    By the end of the book, you’ll be able to build tests pinpointed at Ajax functions, as well as large multi-step tests for the usual suspects: cross-site scripting and injection attacks. This book ...

    ha457-2021-5-12.jar.zip

    Summary IBM HeapAnalyzer is a graphical tool ... a similar feature set (finding large dominators, leak suspects, etc.), and the IBM Extensions for Memory Analyzer with product-specific analysis engines.

    Windows Forensics Cookbook

    Collect and validate evidence from suspects and computers and uncover clues that are otherwise difficult Packed with powerful recipes to perform highly effective field investigations Book Description...

    Android代码-DiscogsBrowser

    A direct link to the latest APK can be found here DiscogsBrowser Native Android app to interact with the Discogs' public API.... This app uses the usual suspects: Dagger2 Retrofit2 Rx

    英语四级单词表

    - **例句:** The police took swift action to arrest the suspects.(警方迅速行动逮捕了嫌疑人。) 47. **active a.** 活跃的;积极的 - **例句:** She leads an active lifestyle and enjoys sports.(她过...

    unusual-suspects

    该项目是通过引导的。 您将在下面找到一些有关如何执行常见任务的信息。 您可以在找到本指南的最新版本。目录自动格式化代码更改页面&lt;title&gt;... 使用AJAX请求获取数据与API后端集成节点Ruby on Rails 在开发中...

    MemoryAnalyzer-1.2.0.20120530-win32.win32.x86_64.zip

    Memory Analyzer (MAT) The Eclipse Memory Analyzer is a fast and feature-rich Java heap analyzer that helps you find memory leaks and reduce memory ... run a report to automatically extract leak suspects.

    高三语法填空无提示词.ppt

    比如2016年的一道题目,其中“while one is being bottle fed, 70.the other is with mum--she never suspects.”这句中,70处的填空就是一个典型的无提示词情况,可能需要填写一个冠词、介词或连词来连接两个并列的...

    高考英语语法填空解题技巧PPT课件.pptx

    例如2016年全国卷I的"The nursery team switches him every few days with his sister so that while one is being bottle-fed, the other is with mum—she never suspects.",这里包含了"so that"引导的结果状语...

    MemoryAnalyzer-1.7.0.20170613-macosx.cocoa.x86_64.zip

    quickly calculate the retained sizes of objects, see who is preventing the Garbage Collector from collecting objects, run a report to automatically extract leak suspects. 检测内存泄漏的利器,协助...

    OpenCV 3 Blueprints

    **1.3 Rounding up the Unusual Suspects(不寻常对象的捕捉)** - **非传统摄像头的应用**:介绍如何利用PlayStation Eye、ASUS Xtion PRO Live等非传统摄像头进行图像采集。 - **Point Grey Research 摄像头**:...

    Eclipse Memory Analyzer中文使用说明(带书签)

    - 分析 dominator tree 和 Leak Suspects:查看并理解 dominator tree 和 Leak Suspects 报表,找出内存占用高的对象和可疑的内存泄漏。 - 优化代码:根据分析结果,优化代码以减少内存占用和避免内存泄漏。 7. ...

    牛津9A Unit5-6测试题精选.doc

    B部分则更侧重于词汇的记忆和拼写,如"enemies"(敌人)、"supplemented"(补充)、"suspects"(嫌疑犯)、"record"(记录)、"wedding"(婚礼)、"remind"(提醒)、"effort"(努力)、"struggle"(搏斗)、...

    某某acm题解(收藏)

    11. **逻辑推理**:1789题"The Suspects"可能包含逻辑推理和条件判断,需要对复杂情况的分析能力。 12. **平均速度计算**:1890题"Average Speed"要求理解速度、时间和距离的关系,以及如何在不同速度下计算平均...

Global site tag (gtag.js) - Google Analytics