How Many Tables-并查集
id=19354" target="_blank" style="color:blue; text-decoration:none">HDU - 1213
Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
id=19354" class="login ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="display:inline-block; position:relative; padding:0px; margin-right:0.1em; vertical-align:middle; overflow:visible; text-decoration:none; font-family:Verdana,Arial,sans-serif; border:1px solid rgb(211,211,211); color:blue; font-size:12px!important">Submit
Description
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
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
Sample Input
2
5 3
1 2
2 3
4 5 5 1
2 5
Sample Output
2
4题目目的非常easy就是让你求有几个集合所以直接模板飘过/*
Author: 2486
Memory: 1420 KB Time: 0 MS
Language: G++ Result: Accepted
*/
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=1000+5;
int a,b,T,N,M,par[maxn],ranks[maxn];
void init(int sizes) {
for(int i=1; i<=sizes; i++) {
par[i]=i;
ranks[i]=1;
}
}
int find(int x) {
return par[x]==x? x:par[x]=find(par[x]);
}
bool same(int x,int y) {
return find(x)==find(y);
}
void unite(int x,int y) {
x=find(x);
y=find(y);
if(x==y)return ;
if(ranks[x]>ranks[y]) {
par[y]=x;
} else {
par[x]=y;
if(ranks[x]==ranks[y])ranks[x]++;
}
}
int main() {
scanf("%d",&T);
while(T--) {
scanf("%d",&N);
scanf("%d",&M);
init(N);
for(int i=0; i<M; i++) {
scanf("%d%d",&a,&b);
unite(a,b);
}
int ans=0;
for(int i=1; i<=N; i++) {
if(par[i]==i)ans++;
}
printf("%d\n",ans);
}
return 0;
}
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 ...
- C - How Many Tables 并查集
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to kn ...
- 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 ...
随机推荐
- react-router路由参数
react-router会自动为路由组件插入三个参数,但是不会主动为路由组件的子组件注入 子组件当中需要在属性当中传入 不是路由组件需要注入路由参数的话,也可以使用withRouter注入,而不是从属 ...
- 2721: [Violet 5]樱花
2721: [Violet 5]樱花 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 547 Solved: 322[Submit][Status][D ...
- sql partition by
--不分班按学生成绩排名 select *,ROW_NUMBER() over(order by Score desc) as Sequence from Student id Gr ...
- 10个HTML和CSS必须知道的重点难点问题
前端日刊 登录 10个HTML和CSS必须知道的重点难点问题 2018-02-26 阅读 2982 收藏 6 原链:segmentfault.com 分享到: 前端必备图书<深入浅出Node ...
- linux 通过MD5监控指定路径文件的变动
脚本须知: 1. 运行此脚本的用户必须是root,因为在某些文件所在路径普通用户没有访问权限 2. 源文件和其md5码只要有一方内容有改动,都会导致校验失败,所以校验码的保存就至关重要防止其他人修改, ...
- 安卓全屏状态下键盘充满屏幕留不出ui控件的解决办法附edittext和键盘的属性
1.我们先看看常用和不常用的属性值(Edittext) android:inputType参数类型说明 android:inputType="none"--输入普通字符 andro ...
- Java IO 学习(一)同步/异步/阻塞/非阻塞
关于IO,同步/异步/阻塞/非阻塞,这几个关键词是经常听到的,譬如: “Java oio是阻塞的,nio是非阻塞的” “NodeJS的IO是异步的” 但是这些东西听多了就容易迷糊,比方说同步是否就是阻 ...
- webpack常用配置项配置文件介绍
一.webpack基础 1.在项目中生成package.json:在项目根目录中输入npm init,根据提示输入相应信息.(也可以不生成package.json文件,但是package.json是很 ...
- Jenkins introduction
http://birdinroom.blog.51cto.com/7740375/1342897 https://www.ibm.com/developerworks/cn/java/j-lo-jen ...
- 【spring boot】配置文件 application.properties 属性解析
1.JPA hibernate命名策略 完整命名策略 ,查看:http://www.cnblogs.com/sxdcgaq8080/p/7910474.html 2.hibernate的DDL执行策 ...