并查集—HDU 1213
来源:互联网 发布:淘宝童装促销文案 编辑:程序博客网 时间:2024/06/06 07:50
题目点这里
题目大意:
A和B认识,那么A和B可以坐在一张桌子上,
A认识B,B认识C,那么A,B,C都可以坐在一张桌子上,桌子不限定人数
最基本的用并查集判断连通性,然后输出连通分量的个数
代码
1. #include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>const int M = 1005;int id[M];int count;int find(int p){ while(p != id[p]) p = id[p]; return p;}bool connected(int p,int q){ return find(p) == find(q);}void unionn(int p,int q){ int pRoot = find(p); int qRoot = find(q); if(pRoot == qRoot) return; id[pRoot] = qRoot; --count;}int main(){ int n,m; int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); count = n; for(int i = 1;i <= n; i++) id[i] = i; while(m--) { int p,q; scanf("%d%d",&p,&q); unionn(p,q); } printf("%d\n",count); } return 0; }
代码还可以优化,带上路径压缩。
做的第一道并查集题目。
0 0
- 并查集—HDU 1213
- hdu 1213 并查集
- hdu 1213 并查集
- hdu 1213并查集
- HDU-1213并查集
- hdu 1213 并查集
- HDU 1213 并查集
- hdu 1213 并查集
- HDU 1213 并查集
- hdu 1213 并查集
- hdu 1213并查集
- hdu 1213 并查集
- HDU--1213并查集
- hdu 1213 并查集
- HDU 1213 并查集
- HDU 1213 并查集
- hdu 1213 (并查集)
- hdu 1213 并查集
- ActiveMQ有支持两种事务
- 重启网卡的几种方法
- CorelDRAW中如何复制对象属性详解
- spring boot 打包和部署
- 最长递增子序列 O(NlogN)算法,mark数组。ZOJ Problem Set
- 并查集—HDU 1213
- 必测的支付漏洞(一)使用fiddler篡改支付金额
- JSP三大指令元素
- amq的queue同步和异步接收消息
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element
- HTTP 协议漫谈
- python time、datetime模块学习使用
- 实验二:Lagrange拉格朗日插值法之C语言代码
- cuda设置指定的GPU可见