How Many Tables

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)

Total Submission(s) : 49   Accepted Submission(s) : 38

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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<stdio.h>
int c,n;
int a[1010];
void f()
{
	int i;
	for(i=1;i<=n;i++)
	a[i]=i;
}
int find(int x)
{
	int r=x;
	while(r!=a[r])
		r=a[r];
	a[x]=r;
	return r;
}
void join(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	a[fx]=fy;
}
int main()
{
	scanf("%d",&c);
	while(c--)
	{
		int q,w,h;
		scanf("%d%d",&n,&h);
		f();
	while(h--)
		{
			scanf("%d%d",&w,&q);
			join(w,q);
		}
		int ans=0;
		for(int i=1;i<=n;i++)
		if(a[i]==i)
		ans++;
		printf("%d\n",ans);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-11 05:36:21

How Many Tables的相关文章

Replicate Partitioned Tables and Indexes

在初始化subscriber时,Replication能够将分区table 和 分区index的Partition function 和 Partition schema 复制到 subscriber中,这样,table 和 Index 以相同的Partition schema创建.但是,replication 不会将 Partition function 和 Partition schema的更新同步到subscriber,即只在初始subscriber时,复制一次 Partition fun

mysql启动报错:Fatal error: Can’t open and lock privilege tables: Table ‘mysql.host’ doesn’t exist

mysql在首次启动的时候可能会报错:Can’t open and lock privilege tables: Table ‘mysql.host’ doesn’t exist 这时候可以执行脚本 mysql_install_db –user=mysql –ldata=数据存放的路径

uva 1385 - Billing Tables(字典树)

题目链接:uva 1385 - Billing Tables 题目大意:给定n个电话前缀,每个前缀是一个区域的前缀,现在要生成一个新的电话单,即对于每个电话号码,从旧的电话单上从前向后遍历,如果出现前缀匹配,则该电话号码对应的即为当前的区号,要求生成的新电话单尽量小. 解题思路:用dfs建立字典树,在区间范围内的点对应均为对应的区号,注意如果70.71.72....79都为SB的话,那么可以合并成7,并且对应区号为SB. 注意合并的条件为区号相同即可,并不是说对应旧电话单匹配位置相同. 注意这组

MySql: show databases/tables use database desc table

1. show databases mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || test |+--------------------+4 rows in set (0.00 sec) 2. use database_name mysql> use testDatabase

People Tools catalog tables.

Projects PSPROJECTDEFN — Project header table PSPROJECTITEM — Definitions in the project Fields PSDBFIELD — Fields in the system PSXLATITEM — Translate Values Records PSRECDEFN — Record header table (record types) PSRECFIELD — Fields in the record (s

对于显示指定LOCK TABLES的并行插入问题(从其它表)

文档: If you acquire a table lock explicitly with LOCK TABLES, you can request a READ LOCAL lock ratherthan a READ lock to enable other sessions to perform concurrent inserts while you have the tablelocked.To perform many INSERT and SELECT operations o

启动mysql问题Can&#39;t open and lock privilege tables: Table &#39;mysql.host&#39; doesn&#39;t exist

启动mysql出现问题 /etc/init.d/mysqld start MySQL server PID file could not be found![FAILED] Starting MySQL......................................The server quit without updating PID file (/data/mysql/mysql_3306/data/mysql.pid).[FAILED] 查看错误日志 150619 15:57:

安装concrete时提示“...database does not support InnoDB database tables...&quot;如何解决

安装很多系统时,经常有有提示: "...database does not support InnoDB database tables..." 解决办法: 找到MySQL的配置文件: windows下是my.ini linux下/etc/my.cnf 找到如下的代码: skip-innodb 修改为如下: #skip-innodb

poj1083 Moving Tables

Moving Tables Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26782   Accepted: 8962 Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure. The floor has 200

1502021733-hdu-How Many Tables

How Many Tables Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 14   Accepted Submission(s) : 12 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Today is Ignatius' bi