C - How Many Tables 并查集
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.
InputThe 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.
OutputFor 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 这个题做麻烦了,其实可以直接遍历pre数组,其中pre[i]==i的个数就是所有集合的个数!
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<vector>
#include<set>
#include<iomanip>
#include<iostream>
using namespace std;
#define MAXN 30000
#define INF 0x3f3f3f3f
/*
并查集
*/
set<int> s;
int pre[MAXN],n,m;
int find(int x)
{
if(x==pre[x])
return x;
else
return pre[x] = find(pre[x]);
}
void mix(int x,int y)
{
int fx = find(x),fy = find(y);
if(fx!=fy)
pre[fy] = fx;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int x,y;
cin>>n>>m;
for(int i=;i<=n;i++)
pre[i] = i;
s.clear();
for(int i=;i<m;i++)
{
cin>>x>>y;
mix(x,y);
}
int cnt = ;
for(int i=;i<=n;i++)
{
int fi = find(i);
if(!s.count(fi))
{
cnt++;
s.insert(fi);
}
}
cout<<cnt<<endl;
}
return ;
}
C - How Many Tables 并查集的更多相关文章
- HDU 1213 - How Many Tables - [并查集模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...
- hdu1213 How Many Tables(并查集)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- POJ-1213 How Many Tables( 并查集 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday. ...
- HDU 1213 How Many Tables(并查集,简单)
题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...
- HDU 1213 How Many Tables (并查集,常规)
并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...
- HDU 1213 How Many Tables 并查集 寻找不同集合的个数
题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...
- hdu1213 How Many Tables 并查集的简单应用
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单的并查集 代码: #include<iostream> #include< ...
- HDU 1213 How Many Tables 并查集 水~
http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...
- 并查集-F - How Many Tables
F - How Many Tables 并查集的模板都能直接套,太简单不注释了,就存个代码 #include<bits/stdc++.h> using namespace std; ; i ...
随机推荐
- PHP无符号右移与旋转右移
# PHP 无符号右移 仅用于int形, PHP 的int为32位 # // 右移旋转 function rightRoate($int,$n){ $min = intval(PHP_INT_MAX ...
- phpcms v9文章内容页调用上一篇下一篇的方法(转)
phpcms v9文章内容页调用上一篇下一篇的方法如下,魔客吧(www.moke8.com)提示您直接摘取如下代码中的红色部分即可: 上一篇:{$previous_page[url]}" t ...
- dutacm.club_1089_A Water Problem_(dp)
题意:要获得刚好后n个'k'的字符串,有两种操作,1.花费x秒,增加或删除1个'k'; 2.花费y秒,使个数翻倍.问最少需要多少时间获得这个字符串. 思路:i为偶数个'k',dp[i]=min(dp[ ...
- HDU_1018_n(1e7)的阶乘的结果的位数
http://acm.hdu.edu.cn/showproblem.php?pid=1018 Big Number Time Limit: 2000/1000 MS (Java/Others) ...
- Angular ZoneJS 原理
Zone.js到底是如何工作的? 原文链接: blog.kwintenp.com 如果你阅读过关于Angular 2变化检测的资料,那么你很可能听说过zone.Zone是一个从Dart中引入的特性并被 ...
- P1541 乌龟棋 题解(洛谷,动态规划递推)
题目:P1541 乌龟棋 感谢大神的题解(他的写的特别好) 写一下我对他的代码的理解吧(哎,蒟蒻就这能这样...) 代码: #include<bits/stdc++.h> #define ...
- java基础学习日志---File方法分析
package FunDemo; import java.io.File; import java.io.IOException; import java.util.Arrays; public cl ...
- CCF201609-2 火车购票 java(100分)
试题编号: 201609-2 试题名称: 火车购票 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 请实现一个铁路购票系统的简单座位分配算法,来处理一节车厢的座位分配. 假设一 ...
- Python 开发面试题
Python部分 将一个字符串逆序,不能使用反转函数 求从10到100中能被3或5整除的数的和 What is Python? What are the benefits of using Pytho ...
- 00 大王警语--be_a_new_gentleman
大王博客:https://www.cnblogs.com/alex3714/ # 表面层次# 1,着装特体(服饰的牌子中高端)# 2,每天洗澡# 3,适当用香水# 4,女士优先# 5,不随地吐痰.不乱 ...