HDU 1213 How Many Tables (并查集,常规)

并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401

题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋友都可以坐同一张桌,没有关系的则要另开一桌,问需要多少张桌子(小明不坐,不考虑小明与其他人的关系)?

思路:常规的并查集。要求出所有人的老大,有几个老大就要几张桌子。那么有关系的都归为同一个老大。用数组实现,再顺便压缩路径。

 1 #include <bits/stdc++.h>
 2 #define LL long long
 3 using namespace std;
 4 const int N=1005;
 5 int pre[N]; //结点i的上级
 6 inline void init(int n){for(int i=1; i<=n; i++) pre[i]=i;}  //将上级初始化为自己
 7
 8 int find(int x) //找x的上级
 9 {
10     int k=x;
11     while(pre[x]!=x)    x=pre[x];   //找上级
12     int tmp;
13     while(pre[k]!=x) //优化:路径压缩
14     {
15         tmp=pre[k];
16         pre[k]=x;
17         k=tmp;
18     }
19     return x;
20 }
21
22 void joint(int a,int b)  //将a和b合并为一个集合
23 {
24     a=find(a);
25     b=find(b);
26     if(a!=b)    pre[a]=b;
27 }
28
29 int check(int n)    //查查到底需要几张桌子
30 {
31     set<int> sett;
32     for(int i=1; i<=n; i++)    find(i); //防漏网之鱼,将所有人的老大推到最顶。
33     for(int i=1; i<=n; i++)    sett.insert(pre[i]);
34     return sett.size();
35 }
36 int main()
37 {
38     //freopen("e://input.txt", "r", stdin);
39     int t, n, m, a, b;
40     cin>>t;
41     while(t--)
42     {
43         scanf("%d%d", &n, &m);
44         init(n);
45         for(int i=0; i<m; i++)
46         {
47             scanf("%d%d",&a,&b);
48             joint(a,b);
49         }
50         printf("%d\n",check(n));
51     }
52     return 0;
53 }

AC代码

时间: 2024-10-26 18:00:22

HDU 1213 How Many Tables (并查集,常规)的相关文章

HDU 1213 How Many Tables (并查集)

How Many Tables Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1213 Appoint description:  System Crawler  (2015-05-25) Description Today is Ignatius' birthday. He invites a lot of friends. Now

HDU 1213 How Many Tables (并查集,连通分支数,两种方式)

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23012    Accepted Submission(s): 11485 Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's din

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

[2016-03-15][HDU][1213][How Many Tables]

时间:2016-03-15 16:19:15 星期二 题目编号:[2016-03-15][HDU][1213][How Many Tables] 题目大意:请朋友吃饭,每个朋友都不喜欢和不认识的人在一桌,给出认识的关系,问至少要多少桌 输入: t组数 每组数据 n m m行 u v 表示u 和 v 认识 输出: 最少 分析:并查集,求集合的数目 #ifdef _WORK_ #include <vector> #include <list> #include <map>

HDU 3635 延缓更新的并查集

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2839    Accepted Submission(s): 1097 Problem Description Five hundred years later, the number of dragon balls will increase unexpecte

HDU 3461 Code Lock(并查集的应用+快速幂)

* 65536kb,只能开到1.76*10^7大小的数组.而题目的N取到了10^7,我开始做的时候没注意,用了按秩合并,uset+rank达到了2*10^7所以MLE,所以貌似不能用按秩合并. 其实路径压缩也可以不用.............  题目的大意: 一个密码锁上有编号为1到N的N个字母,每个字母可以取26个小写英文字母中的一个.再给你M个区间[L,M],表示该区间的字母可以一起同步"增加"(从'a'变为'b'为增1,'z'增1为'a').假如一组密码按照给定的区间进行有限

HDU 1558 Segment set (并查集+线段非规范相交)

题目链接 题意 : 如果两个线段相交就属于同一集合,查询某条线段所属集合有多少线段,输出. 思路 : 先判断与其他线段是否相交,然后合并. 1 //1558 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <cmath> 6 #define eps 1e-8 7 #define zero(x) (((x) > 0 ? (x) : (-x)) < e

HDU 1213 How Many Tables(模板——并查集)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 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 fri

hdu 1213 How Many Tables(并查集的简单应用)

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16333    Accepted Submission(s): 8012 Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinn