1213 How Many Tables(简单并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213
简单并查集,统计单独成树的数量。
代码:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <algorithm>
#include <iostream>
#include <ctype.h>
#include <iomanip>
#include <queue>
#include <map>
#include <stdlib.h>
using namespace std; int f[];
int n,m; void init()
{
for(int i=;i<=n;i++)
f[i]=i;
} int find(int x)
{
if(x==f[x])
return f[x];
f[x]=find(f[x]);
return f[x];
} void Union(int x,int y)
{
int a=find(x);
int b=find(y);
if(a==b)
return;
f[a]=b;
} int main()
{
int t,a,b;
while(cin>>t){
while(t--){
scanf("%d %d",&n,&m);
init();
int sum=;
for(int i=;i<m;i++){
scanf("%d %d",&a,&b);
Union(a,b);
}
for(int i=;i<=n;i++){
if(i==find(i)){
sum++;
}
}
printf("%d\n",sum);
}
}
}
1213 How Many Tables(简单并查集)的更多相关文章
- HDU 1213 How Many Tables (并查集)
How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...
- HDU 1213 How Many Tables(并查集模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...
- HDU - 1213 How Many Tables 【并查集】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意 给出N个人 M对关系 找出共有几对连通块 思路 并查集 AC代码 #include < ...
- 杭电 1213 How Many Tables (并查集求团体数)
Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...
- hdu 1213 How Many Tables(并查集练习)
题目链接:hdu1213 赤裸裸的并查集.....水题一个.... #include<stdio.h> #include<string.h> #include<algor ...
- hdu 1213 How Many Tables(并查集算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/O ...
- HDU 1213 How Many Tables(并查集)
传送门 Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Igna ...
- HDU 1213 How Many Tables(并查集裸题)
Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...
- How Many Tables 简单并查集
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to kn ...
- HDU 1213 How Many Tables【并查集】
解题思路:和畅通工程类似,问最后还剩下几个不连通的区域. How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
随机推荐
- 更改Activity的最底层的布局
public void attachToActivity(Activity activity) { mActivity = activity; TypedArray a = activity.getT ...
- 同一个PC只能运行一个应用实例(考虑多个用户会话情况)
原文:同一个PC只能运行一个应用实例(考虑多个用户会话情况) class Program { private static Mutex m; [STAThread] static void Main( ...
- Oracle 工艺结构
Oracle工艺结构 这个过程是动态创建,完毕任务后就消亡:而程序是静态的实体,程序是能够复制.编辑的.进程强调的是运行过程,而程序不过指令的有序集合:进程在内存中,程序在外存中. ORACLE分为用 ...
- Guest与virtio netdev交互模式
Qemu为virtio设备分配了专门的pci设备ID,device IDs (vendor ID 0x1AF4) from 0x1000 through 0x10FF,而pci子系统中的厂商ID和设备 ...
- 最小二乘法 (转)good
最小二乘法也称为最小平方法,是一种数据优化技术,它通过最小化误差的平方和寻找数据的最佳函数匹配. 最小二乘法最初由高尔顿在创立回归分析的时候提出,现在已经成为探索变量间关系最重要的方法,最小二乘法根据 ...
- Linux lspci查看硬件设备
Linux 主机的硬件配备 lspci 找到的是眼下主机上面的硬件配备 [root@www ~]# lspci [-vvn] 选项与參数: -v :显示很多其它的 PCI 接口装置的具体信息 ...
- 玩转Web之Jsp(三)-----Jsp+SQLServer 用sql语句实现分页
在BBS的实现里,jsp与sqlserver 结合的操作中,怎样实现分页,使每页显示根帖的名字,并按发表时间降序排列? 在这里举例说明,page_size为每页显示的条数,pageNo为当前页数,st ...
- JProgressBar的一个框架
Frame: package swing.progress; import java.awt.BorderLayout; import java.awt.Frame; import java.awt. ...
- 右键菜单中的好友列表Ajax直接跳转请求到登陆页面
今天,我们正在做正确的菜单.当点击重命名Ajax要求,并且不发送数据的背景,但直接跳到主页. 我百思不得其解,后来我发现在头版的一个问题: <li><a href='#' oncli ...
- AsyncTask測试多任务
本人进行过模拟測试,发现AsyncTask并不适合多任务,以及长期的异步任务,由于每次仅仅能执行一个AsyncTask,假设执行多个其他任务将会等待 以下通过一个代码样例和日志打印得到证实. 以下扩展 ...