2018学校暑期集训第五天——并查集 线段树

练习题B  ——   HDU - 1213

How Many Tables


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 55 1
2 5

Sample Output

2
4

#include<iostream>
using namespace std;int par[1010];
int ran[1010];void init(int n)
{for(int i=1; i<=n; i++){par[i]=i;ran[i]=0;}
}int find(int x)
{if(par[x]==x)return x;elsereturn par[x]=find(par[x]);
}void unite(int x, int y)
{x=find(x);y=find(y);if(x==y)return;if(ran[x]<ran[y]){par[x]=y;}else{par[y]=x;if(ran[x]==ran[y])ran[x]++;}
}int main(void)
{int t, m, n, a, b;cin >> t;while(t--){scanf("%d%d", &n, &m);init(n);for(int i=0; i<m; i++){scanf("%d%d", &a, &b);if(a<b)swap(a, b);unite(a, b);}int sum=0;for(int i=1; i<=n; i++){if(par[i]==i)sum++;}printf("%d\n", sum);getchar();}
}

暑期集训5:并查集 线段树 练习题B: HDU - 1213 ​​​​​​​相关推荐

  1. 暑期集训5:并查集 线段树 练习题G: HDU - 1754

    2018学校暑期集训第五天--并查集 线段树 练习题G  --   HDU - 1754 I Hate It 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.  这让 ...

  2. 暑期集训5:并查集 线段树 练习题F:  HDU - 1166 ​​​​​​​

    2018学校暑期集训第五天--并查集 线段树 练习题F  --   HDU - 1166 敌兵布阵 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A ...

  3. 暑期集训5:并查集 线段树 练习题A:  HDU - 1232 ​​​​​​​

    2018学校暑期集训第五天--并查集 线段树 练习题A  --   HDU - 1232 畅通工程 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅 ...

  4. UVA1455 - Kingdom(并查集 + 线段树)

    UVA1455 - Kingdom(并查集 + 线段树) 题目链接 题目大意:一个平面内,给你n个整数点,两种类型的操作:road x y 把city x 和city y连接起来,line fnum ...

  5. BZOJ 3910 并查集+线段树合并

    思路: 1. 并查集+线段树合并 记得f[LCA]==LCA的时候 f[LCA]=fa[LCA] 2.LCT(并不会写啊...) //By SiriusRen #include <cstdio& ...

  6. 【Codeforces576E_CF576E】Painting Edges(可撤销并查集+线段树分治)

    题目 CF576E 分析: 从前天早上肝到明天早上qwq其实颓了一上午MC ,自己瞎yy然后1A,写篇博客庆祝一下. 首先做这题之前推荐一道很相似的题:[BZOJ4025]二分图(可撤销并查集+线段树 ...

  7. 线段树分治 ---- CF1217F - Forced Online Queries Problem(假离线 可撤销并查集 + 线段树分治)详解

    题目链接 题目大意 解题思路: 我一开始想到可以用可撤销并查集去维护这种删边加边的操作,但是有个缺点是每次撤销都有把后面的边全部撤销复度是O(n2)O(n^2)O(n2) 首先我们考虑这种动态加边删边 ...

  8. poj 2985(并查集+线段树求K大数)

    解题思路:这道题并查集很容易,合并时找到父节点就直接加上去就ok了.关键是如何求K大数,我一直在想用线段树怎么写,一开始想如果直接记录数的大小那肯定是没戏了,借鉴了一下别人的思路:区间[a,b]记录的 ...

  9. 【BZOJ4025】二分图(可撤销并查集+线段树分治)

    题目: BZOJ4025 分析: 定理:一个图是二分图的充要条件是不存在奇环. 先考虑一个弱化的问题:保证所有边出现的时间段不会交叉,只会包含或相离. 还是不会?再考虑一个更弱化的问题:边只会出现不会 ...

最新文章

  1. 读8篇论文,梳理BERT相关模型进展与反思
  2. Python中的collections模块的相关练习
  3. Redis 敢在线上做Keys正则匹配操作!你可以离职了!
  4. 学习笔记(一)——图像的灰度级和动态范围
  5. POJ - 2248 Addition Chains(dfs+迭代加深)
  6. 【算法系列之四】柱状图储水
  7. mysql ---- 多表设计
  8. 如何对动态创建控件进行验证以及在Ajax环境中的使用
  9. Linux内核空间内存申请函数kmalloc、kzalloc、vmalloc的区别【转】
  10. python人口普查数据数据分析_7、Python 数据分析-Pandas人口分析案例
  11. mysql 笛卡尔积
  12. 简述sqlite数据库的特点_sqlite数据库特点
  13. 我用Vue3+TS实现了一个新年倒计时组件,适用于各种场景
  14. android cue文件播放器,手机cue文件怎么打开(支持cue的安卓播放器)
  15. ASP.NET Core 导出Excel文件
  16. Eclipse护眼背景及字体设置
  17. 21年Android面经分享,大厂面试题汇总
  18. 650c公路车推荐_菜鸟篇-公路自行车入门推荐及选购指南
  19. [知乎]停不下来的笑话
  20. win7的系统怎么进入win10服务器,肿么把win7系统升级到win10

热门文章

  1. java使用jeids实现redis2.6的String操作(1)
  2. Android+git+hudson+gradle持续集成
  3. java web 自定义filter
  4. Global.asax
  5. jquery后学什么_我在训练营两年后学到了什么
  6. angular react_Angular 2 vs React:将会有鲜血
  7. linux程序计数器,如何在C中打印程序计数器的确切值
  8. 新建异常并处理java_java – 动态创建异常的工厂模式
  9. Python--数据存储:pickle模块的使用讲解
  10. 初学Java——选择