How Many Tables(POJ 1213 求连通分量)

How Many Tables

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

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

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <set>
 6 #include <cmath>
 7 using namespace std;
 8 #define Max 1000+10
 9 int per[Max];
10 int n,m;
11 void init()
12 {
13     for(int i=1;i<=n;i++)
14         per[i]=i;
15 }
16 int find(int a)
17 {
18     if(a==per[a])
19         return a;
20     /*int root=a,x=a,j;
21     while(root!=per[root])    root=per[root];
22     while(x!=per[x])
23     {
24         j=per[x];
25         per[x]=root;
26         x=j;
27     }*/
28     return per[a]=find(per[a]);
29 }
30 int unite(int a,int b)
31 {
32     a=find(a);
33     b=find(b);
34     if(a!=b)
35         per[a]=b;
36     return 0;
37 }
38 int main()
39 {
40     int t,a,b;
41     freopen("in.txt","r",stdin);
42     scanf("%d",&t);
43     while(t--)
44     {
45         int ans=0;
46         scanf("%d%d",&n,&m);
47         init();
48         for(int i=1;i<=m;i++)
49         {
50             scanf("%d%d",&a,&b);
51             unite(a,b);
52         }
53         for(int i=1;i<=n;i++)
54             if(per[i]==i)
55                 ans++;
56         printf("%d\n",ans);
57     }
58 }

Author

Ignatius.L

时间: 2024-08-08 22:08:54

How Many Tables(POJ 1213 求连通分量)的相关文章

poj 2524 求连通分量(并查集模板题)

求连通分量 Sample Input 10 91 21 31 41 51 61 71 81 91 1010 42 34 54 85 80 0Sample Output Case 1: 1Case 2: 7 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # include

hdu 1213 求连通分量(并查集模板题)

求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # include <queue> 7 # define LL long

A - Network of Schools - poj 1236(求连通分量)

题意:学校有一些单向网络,现在需要传一些文件,1,求最少需要向几个学校分发文件才能让每个学校都收到,2,需要添加几条网络才能在任意一个学校分发都可以传遍所有学校. 分析:首先应该求出来连通分量,进行缩点,然后求每个分量的入度和出度,入度等于0的很明显都需要分发一个文件,至于需要添加几条边可以成为一个强连通,就是出度和入度最大的那个,因为需要把出度和入度相连. **************************************************************** #inc

poj 3352 求 边-双连通分量

[题意] 给出一张无向连通图,求至少连几条边可以变成边双连通图 [思路]求出边-双连通分量,缩点就成了一棵树,求这棵树里的出度为1 的点num  结果是(num-1)/2; 1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<stack> 5 #include<vector> 6 using namespace std; 7 int pre[1002],

poj 3177 求至少添加多少条边可以成为边-双连通图(有重边)

[题意]:给出一张无向连通图,求添加多少条边可以成为边-双连通图 [思路]:同3352 一样,求出边-双连通分量,缩点就成了一棵树,求这棵树里的出度为1 的点num  结果是(num-1)/2; 但是!!  这里和3352 哟一点不一样就是这里有重边,当有重边的时候,不同low值的两点可能属于同一个边-双连通分量 所以在构图的时候要注意把重边去掉! 1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h>

POJ 2104 求序列里第K大 主席树裸体题

给定一个n的序列,有m个询问 每次询问求l-r 里面第k大的数字是什么 只有询问,没有修改 可以用归并树和划分树(我都没学过..囧) 我是专门冲着弄主席树来的 对主席树的建树方式有点了解了,不过这题为什么是在主席树里面这么操作的 还是有点不懂,今天照着模板敲了一遍就打多校了 再研究吧 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using name

POJ 2449 求第K短路

第一道第K短路的题目 QAQ 拿裸的DIJKSTRA + 不断扩展的A* 给2000MS过了 题意:大意是 有N个station 要求从s点到t点 的第k短路 (不过我看题意说的好像是从t到s 可能是出题人写错了) 从这题中还真的学到了很多1.第k短路的算法 A* 还有用边表实现dij (注:以下部份资料来源于网上)所谓A*就是启发是搜索 说白了就是给搜索一个顺序使得搜索更加合理减少无谓的搜索. 如何来确定搜索的顺序?..也就是用一个值来表示 这个值为f[n]..每次搜索取f[x]最小的拓展 那

poj 1279 求半平面交的 面积

poj 1279    求半平面交的 面积 题目来源: http://poj.org/problem?id=1279 分析: 求半平面交的 面积 代码如下: const double EPS = 1e-8; const int Max_N = 1505; struct Point{ double x,y; Point(){} Point(double x, double y):x(x),y(y){} Point operator - (Point p){ return Point(x- p.x

kuangbin专题五:C - How Many Tables HDU - 1213

C - How Many Tables HDU - 1213 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