首页 > 其他分享 >How Many Tables

How Many Tables

时间:2023-05-05 18:31:42浏览次数:41  
标签:Tables Ignatius knows tables int Many stay How rrank


How Many Tables



Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)


Total Submission(s): 16865 Accepted Submission(s): 8270




Problem Description


Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.

One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.

For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.





Input


The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.





Output


For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.





Sample Input


2
5 3
1 2
2 3
4 5

5 1
2 5





Sample Output


2 4


分析:并查集简单应用


源代码:


#include <iostream>
using namespace std;
int father[1010],rrank[1010];
void init(int n)
{
	int i;
	for(i=1;i<=n;i++)
	{
		father[i]=i;
		rrank[i]=1;
	}
}
int Find_Set(int x)
{
	if(x!=father[x])
	  return Find_Set(father[x]);
	return father[x];
}
void Union_Set(int x,int y)
{
	x=Find_Set(x);
	y=Find_Set(y);
	if(x==y)  return ;
	if(rrank[x]>=rrank[y])
	{
		father[y]=x;
		rrank[x]+=rrank[y];
	}
	else
	{
		father[x]=y;
		rrank[y]+=rrank[x];
	}
}
int main()
{
	int t,n,m,i,a,b,cnt;
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		cnt=0;
		init(n);
		for(i=0;i<m;i++)
		{
			cin>>a>>b;
			Union_Set(a,b);
		}
		for(i=1;i<=n;i++)
		{
			if(i==father[i])
			  cnt++;
		}
		cout<<cnt<<endl;
	}
	return 0;
}

标签:Tables,Ignatius,knows,tables,int,Many,stay,How,rrank
From: https://blog.51cto.com/u_16099425/6247414

相关文章

  • 面试 v-if 和 v-show的区别
    v-if vs. v-show​v-if 是“真实的”按条件渲染,因为它确保了在切换时,条件区块内的事件监听器和子组件都会被销毁与重建。v-if 也是惰性的:如果在初次渲染时条件值为false,则不会做任何事。条件区块只有当条件首次变为true时才被渲染。相比之下,v-show 简单许多,元素无论初......
  • 《CTFshow-Web入门》08. Web 71~80
    目录web71知识点题解web72知识点题解web73题解web74题解web75知识点题解web76题解web77知识点题解web78知识点题解web79题解web80知识点题解ctf-web入门web71知识点ob_get_contents():得到输出缓冲区的内容。ob_end_clean():清除缓冲区的内容,并将缓冲区关闭,但不会输出内......
  • 批量更改showdoc的图片的URL
    下载SQLiteSpy:http://xz.w10a.com/Small/SQLiteSpyzwb.rar用它打开:\showdoc\Sqlite\showdoc.db.php参考下图定位至“1”处;键入以下命令(红字为原始内容,绿字为替换后的内容)按F9。updatepagesetpage_content=replace(page_content,"192.168.0.111","192.168.X.201")......
  • 230501 TI - Engineer It - How to test power supplies - Overview
    Hi,I'mBobHanrahan,ApplicationEngineeringatTexasInstruments.Thisisthefirstonaseriesontestingpowersupplies.We'llbetalkingaboutsomeofthebasictests,notall,butthebasictestsneededtoensureareliablepowersupply.......
  • 《CTFshow-Web入门》07. Web 61~70
    目录web61~65题解web66知识点题解web67知识点题解web68知识点题解web69知识点题解web70知识点题解ctf-web入门web61~65题解这几个题都和web58一样。可能内部禁用的函数不一样吧。但payload都差不多。不多解释了。以下解法随便挑一个即可。可能不同题会有部分函数被......
  • 索引-性能分析-show profiles
    Sql性能分析:profiles详情:showprofiles能够在做SQL优化时帮助我们了解时间都耗费到哪里去了。通过hava——profiles参数,能够看到当前Mysql是否支持profiles操作执行一系列的业务SQL业务,然后通过如下指令查看指令的执行耗时:#查看每一条SQL的基本耗时情况:showprofiles;#......
  • Think Python-How to Think Like a Computer Scientist_chapter4_练习 4-3
    #coding=gbkimportmathimportturtlebob=turtle.Turtle()print(bob)defpie(t,r,n):"""画一个包含n个三角形的饼图。t:Turtleobjectr:三角形腰长n:包含几个三角形或几边形"""angle1=180/nangle2=90+angle1y=......
  • Tool-CMake-How CMake simplifies the build process by Bruno Abinader
    Tool-CMake-HowCMakesimplifiesthebuildprocessbyBrunoAbinaderhttps://gitlab.kitware.com/cmake/community/-/wikis/homehttps://brunoabinader.github.io/2009/12/07/how-cmake-simplifies-the-build-process-part-1-basic-build-system/https://brunoabin......
  • [ABC140F] Many Slimes
    2023-02-13题目题目传送门翻译翻译难度&重要性(1~10):6题目来源AtCoder题目算法贪心解题思路用了两个multiseta和一个sets,一个multiset用来记录用来存还剩哪些数没生成,另一个用来存已经生成了哪些数,然后后面放数的时候就枚举第二个multiset来生成新的数。然......
  • 在使用showModalDialog中为解决刷新时弹出新窗口时用到iframe所带来的一个问题
    问题描述:我们在开发过程中使用showModalDialog来产生一个弹出窗口,而在这个弹出窗口中要做一个刷新,或者是切换到其它的url时会弹出新窗口。为了解决这个问题,网上有个办法是采用iframe,在showModalDialog窗口中使用iframe这样就不会有弹出窗口了,但这样使用又带来了一个小的问题,我们页......