HDU 1232 并查集板子题
Input测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。
注意:两个城市之间可以有多条道路相通,也就是说
3 3
1 2
1 2
2 1
这种输入也是合法的
当N为0时,输入结束,该用例不被处理。
Output对每个测试用例,在1行里输出最少还需要建设的道路数目。
Sample Input
4 2
1 3
4 3
3 3
1 2
1 3
2 3
5 2
1 2
3 5
999 0
0
Sample Output
1
0
2
998 Huge input, scanf is recommended.
并查集的板子题 ans 一开始应该要初始化为n-1, n个点之间连接只需要n-1条道路
每在两个点之间加入新的道路就 ans --
#include<cstdio>
#include<cstring>
using namespace std; int pre[], ans;
int father(int root){
int son = root, tmp;
while(root != pre[root])
root = pre[root];
while(son != root){
tmp = pre[son];
pre[son] = root;
son = tmp;
}
return root;
}
void add(int x,int y){
int a = father(x);
int b = father(y);
if(pre[a]!=pre[b]){
pre[a] = b;
ans --;
}
}
int main(){
int n, m, x, y;
while(scanf("%d%d",&n,&m)==){
ans = n - ;
for(int i=;i<=n;i++)
pre[i] = i;
for(int i=;i<m;i++){
scanf("%d%d",&x,&y);
if(pre[x] == pre[y])
continue;
add(x,y);
}
printf("%d\n",ans);
}
return ;
}
HDU 1232 并查集板子题的更多相关文章
- 畅通工程 HDU - 1232 并查集板子题
#include<iostream> #include<cstring> using namespace std; ; int p[N]; int find(int x) { ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- HDU 1232 - 并查集 解题报告
畅通project Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- hdu 1213(并查集模版题)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Zjnu Stadium HDU - 3047 带权并查集板子题
#include<iostream> #include<cstring> #include<cstdio> using namespace std; +; int ...
- CodeForces 893C (并查集板子题)
刷题刷到自闭,写个博客放松一下 题意:n个人,m对朋友,每寻找一个人传播消息需要花费相应的价钱,朋友之间传播消息不需要花钱,问最小的花费 把是朋友的归到一起,求朋友中花钱最少的,将所有最少的加起来. ...
- HDU 1232 并查集
畅通工程 Time ...
- HDU 1213(并查集)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu1213 并查集板子
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1213/ 并查集是一种支持合并与查找的数据结构,在森林中进行操作,加上路径压缩,合并和查找的时间复杂度几乎都是常数 ...
随机推荐
- ubuntu18.04安装搜狗输入法
首先,安装Fcitx输入框架 sudo apt install fcitx 其次,上搜狗输入法官网下载Linux版本搜狗输入法(32位和64位根据自己情况,在虚拟机上用浏览器下载即可 然后进入相应的下 ...
- 分布式网上商城项目- BeanDefinitionStoreException
BeanDefinitionStoreException: 严重: StandardWrapper.Throwable org.springframework.beans.factory.BeanDe ...
- yii学习笔记(5),视图操作
在控制器调用$this->render()方法来输出视图 function actionLogin(){ $name = "admin"; // 加载视图 return $t ...
- Linux常用97条命令
1.ls [选项] [目录名 | 列出相关目录下的所有目录和文件 -a 列出包括.a开头的隐藏文件的所有文件 -A 通-a,但不列出"."和".." -l ...
- python3 练习题100例 (六)
题目六:斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.34.……. #!/usr/bin/env python3 ...
- mac安装ruby-oci8
1.安装xcode 2.从oracle官网下载以下安装包 instantclient-basic-macos.x64-12.1.0.2.0.zip instantclient-sdk-macos.x6 ...
- 542. 01 Matrix
class Solution { public: vector<vector<int>> res; int m, n; vector<vector<int>& ...
- Git安装配置(Windows)
下载Git并安装 下载地址:https://git-scm.com/ 安装一般默认即可 配置用户信息 配置之前最好已经有了Github的账号,如果没有可以先去注册一个 安装后打开Git Bash gi ...
- go基础语法-指针
1.基础定义 golang的指针没有cpp等语言的指针复杂,具体表现在其不可用于运算.只有值传递 语法:var variableName *int = memoryAddr var a = 2 var ...
- verilog中参数传递与参数定义中#的作用(二)
一.module内部有效的定义 用parameter来定义一个标志符代表一个常量,称作符号常量,他可以提高程序的可读性和可维护性.parameter是参数型数据的关键字,在每一个赋值语句的右边都必须是 ...