How Many Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11038    Accepted Submission(s): 5447

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
 
Author
Ignatius.L
 
Source
 
 #include <stdio.h>
int pre[];
int find(int x)
{
int i,r,t;
t=r=x;
while(r!=pre[r])
{
r=pre[r];
}
while(t!=r)
{
i=pre[t];
pre[t]=r;
t=i;
}
return r;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int m,n,i,j,a,b,pa,pb,total;
scanf("%d %d",&n,&m);
total=n;
for(i=;i<=n;i++)
pre[i]=i;
for(i=;i<m;i++)
{
scanf("%d %d",&a,&b);
pa=find(a);
pb=find(b);
if(pa!=pb)
{
pre[pb]=pa;
total--;
}
}
printf("%d\n",total);
}
return ;
}

//并查集简单应用

hdu_1213_How Many Tables_201403091126的更多相关文章

随机推荐

  1. SmartDispatcher 类

    UI线程中使用 public class SmartDispatcher { public static void BeginInvoke(Action action) { if (Deploymen ...

  2. java replaceAll 忽略大小写

    public static void main(String[] args) { String temp=" CLASS_path : /aaabb/"; System.out.p ...

  3. 数据库登陆失败原因: 未与信任 SQL Server 连接相关联

    解决方案:用户 'sa' 登录失败.原因: 未与信任 SQL Server 连接相关联. 问题简述: 用户 'sa' 登录失败.原因: 未与信任 SQL Server 连接相关联. 说明: 执行当前 ...

  4. Linux命令补充及基础优化。

    1.用户部分 1.1 创建新用户 涉及命令 useradd [root@oldboyedu-50 ~]# useradd oldboy #添加用户 oldboy 1.2 设置密码 [root@oldb ...

  5. JavaScript中的substr和Java中的substring

    JavaScript::substr(index, length)从下标开始截取多少位,如果length为空,则截取到最后,-1倒数第一位,-2倒数第二位.... Java:substring(sta ...

  6. yield from (python生成器)

    #生成器中的yield from是干什么用的(一般多用于线程,协程那)def func(): # for i in 'AB': # yield i yield from 'AB' # 就相当于上面的f ...

  7. 怎么在windows上安装 ansible How to install ansible to my python at Windows

    答案是不能再window上安装,答案如下: It's back! Take the 2018 Developer Survey today » Join Stack Overflow to learn ...

  8. action="post" 、 servletconfig 、 servletcontext 、getPrintWiter() 、context-param、 init-param(第一个完整的servlet)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  9. Servlet访问路径的两种方式、Servlet生命周期特点、计算服务启动后的访问次数、Get请求、Post请求

    Servlet访问路径的两种方式: 1:注解 即在Servlet里写一个@WebServlet @WebServlet("/myServlet") 2:配置web.xml < ...

  10. 海量文本信息查Top-k

    问题描述: 有1千万条短信,一条一行,有重复.在5分钟之内,找出重复出现的前10条. 方案一: 1.分组进行边扫描边建散列表.建立哈希表,使用头,尾和中间随便两个字节作为Hash Code, 插入到H ...