1213 How Many Tables 简单的并查集问题
my code:
#include <cstdio>
#include <cstring>
#include<iostream>
using namespace std;
int find(int num,int A []){
while(num!=A[num])//{
num = A[num];
return num;
}//
bool follow(int a,int b,int A[]){
int i = find(a,A);
int j = find(b,A);
//cout<<"zxd";
//cout<<i<<" "<<j<<" ";
if(i == j)return false;
//cout<<"zxd";
A[i] = j;
return true;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
//下面的两个变量是人数和关系两个变量
int m,n;
scanf("%d%d",&m,&n);
//在这里还是先创建一个动态数组,来保存返回值
int *A = new int [m+1];
for(int i = 1;i <= m;i++)A[i]=i;//for(int i = 1;i <= m;i++)cout<<A[i];
int a,b,count = 0;
for(int i = 0; i < n; i++){
scanf("%d%d",&a,&b);
// cout<<"循环中ing"<<a<<b<<endl;
if(follow(a,b,A))count++;
// cout<<"count++;"<<count<<endl;
}
printf("%d\n",m - count);
delete [] A;
}
return 0;
}
//在这里有两个关键的函数,也就是PPT中的精华部分
int find(int a)
//查找根结点
{
while(a != fa[a])
//这里要说明的一点是fa[a]是用来判断自己的上一个节点是什么,最后一个东西就是默认根节点的根节点是自己本身
a = fa[a];
return a;
}
int find(int a)
{
return fa[a] == a?a:fa[a] = find(fa[a]);
//在找到根节点的同时压缩了路径 ,将保存父节点的数组更改为根节点
}
union(u,v)
{
fa_u = find(u);
fa_v = find(v);
if(fa_u != fa_v)
fa[fa_u] = fa_v;
}
1213 How Many Tables 简单的并查集问题的更多相关文章
- HDU 1213 How Many Tables(模板——并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...
- hdu 1213 (How Many Tables)(简单的并查集,纯模板)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- The Suspects 简单的并查集
Description 严重急性呼吸系统综合症( SARS), 一种原因不明的非典型性肺炎,从2003年3月中旬开始被认为是全球威胁.为了减少传播给别人的机会, 最好的策略是隔离可能的患者. 在Not ...
- The Suspects(简单的并查集)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- hdu 1182 A Bug's Life(简单种类并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋 这是一道简单的种类并查集,而且也 ...
- HDU1213How Many Tables(基础并查集)
HDU1213How Many Tables Problem Description Today is Ignatius' birthday. He invites a lot of friends. ...
- UVA - 1160(简单建模+并查集)
A secret service developed a new kind of explosive that attain its volatile property only when a spe ...
- HDU1213最简单的并查集问题
题目地址 http://acm.hdu.edu.cn/showproblem.php?pid=1213 #include<iostream> using namespace std; #d ...
- HDU - 1213 dfs求联通块or并查集
思路:给定一个无向图,判断有几个联通块. AC代码 #include <cstdio> #include <cmath> #include <algorithm> ...
随机推荐
- hdu1664 bfs+余数判重
input n 不超过50个例子,n==0结束输入 Sample Input 7 15 16 101 0 output 最少个不同数字的n的倍数的x,若不同数字个数一样,输出最小的x Sample O ...
- datables的基本操作
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- javaWEB总结(13):域对象的属性操作
前言 本文主要讲解javaweb的四个域对象以及他们的作用范围,后面会有小demo来具体测试. 四个域对象 (1)pageContext:属性的作用范围仅限于当前JSP页面: (2)request:属 ...
- mobiscroll 插件札记(一)
mobiscroll 插件笔记(一) 文章参照 http://www.cnblogs.com/headwolf/archive/2013/12/23/3487207.html 最近切一个移动页面,需 ...
- POJ 1678 I Love this Game!#dp博弈
http://poj.org/problem?id=1678 #include<iostream> #include<cstdio> #include<cstring&g ...
- hdu_4787_GRE Words Revenge(在线AC自动机)
题目链接:hdu_4787_GRE Words Revenge 题意: 总共有n个操作,2种操作.每行读入一个字符串. 1.如果字符串以+开头,此为单词(即模式串,不考虑重复) 2.如果字符串以?开头 ...
- Twitter Storm源代码分析之Nimbus/Supervisor本地目录结构
storm集群里面工作机器分为两种一种是nimbus, 一种是supervisor, 他们通过zookeeper来进行交互,nimbus通过zookeeper来发布一些指令,supervisor去读z ...
- 广播与P2P通道(下) -- 方案实现
在广播与P2P通道(上) -- 问题与方案 一文中,我们已经找到了最优的模型,即将广播与P2P通道相结合的方案,这样能使服务器的带宽消耗降到最低,最大节省服务器的宽带支出.当然,如果从零开始实现这种方 ...
- SignalR的坑爹细节
好吧!SignalR的确是好用,照着官网的例子自己敲了一遍,死活得不到效果... 检查了半天,抱着试一试的心态吧原来在服务端大写开头的Hub类和大写开头的方法在客户端调用的时候,全部改成小写,一刷新, ...
- .Net Core Session验证码
1.验证码帮助类 namespace IdeaCore.Services.Common { public class ValidateCodeService : IValidateCodeServic ...