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 ...
随机推荐
- bzoj 3101 N皇后构造一种解 数学
3101: N皇后 Time Limit: 10 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 70 Solved: 32[Submit][S ...
- 利用$.getJSON() 跨域请求操作
原文发布时间为:2011-01-14 -- 来源于本人的百度文章 [由搬家工具导入] $.get 没有权限? $.post 没有权限? 因为他们都不能跨域,那就用 $.getJSON() 吧 利用$. ...
- 【转】SqlCacheDependency的使用 强大的功能
原文发布时间为:2009-10-25 -- 来源于本人的百度文章 [由搬家工具导入] 最近我在忙于研究负载平衡、并发性容错性等性能优化问题,ASP.NET有太多强大的功能等待学习和挖掘。今天, ...
- [LeetCode] Path Sum II 深度搜索
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- Understand:高效代码静态分析神器详解(一)【转】
转自:http://www.codemx.cn/2016/04/30/Understand01/ 之前用Windows系统,一直用source insight查看代码非常方便,但是年前换到mac下面, ...
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---25
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- hdu 4517(递推枚举统计)
小小明系列故事——游戏的烦恼 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)To ...
- DB2 数据库中字段特定字符替换为空
Update RM_CarInfo set ImportTitle = Replace(ImportTitle,'ZD','') WHERE ImportTitle LIKE'%ZD%';
- Codeforces Gym100735 I.Yet another A + B-Java大数 (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)
I.Yet another A + B You are given three numbers. Is there a way to replace variables A, B and C with ...
- Timeout watchdog using a standby thread
http://codereview.stackexchange.com/questions/84697/timeout-watchdog-using-a-standby-thread he simpl ...