`
Simone_chou
  • 浏览: 185447 次
  • 性别: 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

    用最简洁的语言实现了一下并查集,欢迎下载,如有意见或建议可以提出,谢谢大家的支持!:) #include using namespace std; int n,m,k; struct TreeNode { int parent; int num; }T[100000]; void init() //并...

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

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

    Web Security Testing Cookbook

    pinpointed at Ajax functions, as well as large multi-step tests for the usual suspects: cross-site scripting and injection attacks. This book helps you: Obtain, install, and configure useful-and free-...

    ZOJ Problem Set – 2003 Substitution Cipher

    The ACM director suspects one of his competitors of reading his correspondece. To prevent other companies from revealing his secret, he decided to use a substitution cipher in all the letters ...

    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 ...

    poj经典数据结构题目解题报告

    Net&lt;br&gt;Pku acm 3278 Catch That Cow&lt;br&gt;Pku acm 2253 Frogger&lt;br&gt;Pku acm 1062 昂贵的聘礼 Pku acm 1125 Stockbroker Grapevine Pku acm 1611 The Suspects Pku acm 2492 A Bug's Life 更多请访问:...

    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

    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.

    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. 检测内存泄漏的利器,协助...

    ml-glossary:机器学习词汇

    机器学习词汇 寻找其他维护者! 很抱歉我没有回应。 :(我去过Cruise公司,为ML infra提供自动驾驶汽车,而且还没有永远审查此回购协议。...# Assumes you have the usual suspects installed: numpy, sc

    MemoryAnalyzer-1.9.1.20190826-linux.gtk.x86_64.zip

    官网一直下载不了,找这个找了好久,因为生成的dump文件太大不得不用linux..../ParseHeapDump.sh java_pid1089.hprof org.eclipse.mat.api:suspects org.eclipse.mat.api:overview org.eclipse.mat.api:top_components

    19春第三题 PAT甲级 1158 Telefraud Detection (25分) 用这个方法最好

    题目 Telefraud(电信诈骗) remains a ... To stop this crime, you are supposed to write a program to detect those suspects from a huge amount of phone call records. A person must be detected as a sus

Global site tag (gtag.js) - Google Analytics