查环操作,裸题。一次AC。

 #include <iostream>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <string>
#include <cstdio>
#include <algorithm>
#include <numeric>
using namespace std; const int maxnn = 1e4 + ;
int cnt = , father[maxnn]; int getFather (int x) {
if (father[x] != x) {
father[x] = getFather (father[x]);
}
return father[x];
} void Union (int p, int q) {
int x = getFather (p);
int y = getFather (q);
if (x != y) {
father[y] = x;
} else {
cnt ++;
}
} int main () {
int n, x;
while (cin >> n >> x) {
for (int i = ; i < x; ++ i) {
father[i] = i;
}
cnt = ;
for (int i = ;i < x; ++ i) {
int p, q; scanf("%d%d", &p, &q);
Union (p, q);
} /*for (int i = 0; i < n; ++ i) {
if (getFather (i) == i) {
ans ++;
}
}*/
cout << cnt << endl;
}
return ;
}

【HDU2120】Ice_cream's world I(并查集基础题)的更多相关文章

  1. 【HDU1231】How Many Tables(并查集基础题)

    什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...

  2. 【HDU1232】畅通工程(并查集基础题)

    裸敲并查集,很水一次AC #include <iostream> #include <cstring> #include <cstdlib> #include &l ...

  3. 【HDU1856】More is better(并查集基础题)

    裸并查集,但有二坑: 1.需要路径压缩,不写的话会TLE 2.根据题目大意,如果0组男孩合作的话,应该最大的子集元素数目为1.所以res初始化为1即可. #include <iostream&g ...

  4. 【HDU1272】小希的迷宫(并查集基础题)

    仍旧裸敲并查集.有这两点注意: 1.输入 0 0 时候要输出YES 2.留心数组的初始化 #include <iostream> #include <cstring> #inc ...

  5. 【HDU1325】Is It A Tree?(并查集基础题)

    有以下坑点: 1.结束输入不一定-1,题目中的叙述只是说所有权值都为正值. 2.是否构成一棵树不能只判断是否只有一个根节点,没有环路,而且还需要判断每个节点的入度一定是1,不然就不是一棵树. (无环路 ...

  6. poj1182 食物链(并查集 好题)

    https://vjudge.net/problem/POJ-1182 并查集经典题 对于每只动物创建3个元素,x, x+N, x+2*N(分别表示x属于A类,B类和C类). 把两个元素放在一个组代表 ...

  7. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

  8. Brain Network (easy)(并查集水题)

    G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  9. PAT甲级 并查集 相关题_C++题解

    并查集 PAT (Advanced Level) Practice 并查集 相关题 <算法笔记> 重点摘要 1034 Head of a Gang (30) 1107 Social Clu ...

随机推荐

  1. App架构设计经验谈:接口的设计

    App与服务器的通信接口如何设计得好,需要考虑的地方挺多的,在此根据我的一些经验做一些总结分享,旨在抛砖引玉. 安全机制的设计 现在,大部分App的接口都采用RESTful架构,RESTFul最重要的 ...

  2. git术语解释staging,index,cache

    当我在使用git的时候,有三个东西的出现,一度让我非常困扰,就如题所述,staging,index,和cache. 比如,当我阅读git官网提供的电子书<Pro Git>的时候,最初一章里 ...

  3. genymotion+Oracle VM VirtualBox + eclipse + appium 脚本运行慢解决步骤

    genymotion+Oracle VM VirtualBox + eclipse + appium 脚本运行慢解决步骤 1.lenove 机器启动时按F1 进入bios 设置,设置cpu virtu ...

  4. vue + vue-resource 跨域访问

    使用vue + vue-resource进行数据提交,后台使用RESTful API的方式存取数据,搞了一天,终于把后台搞好了.进行联合调试时,数据不能提交,报403错误: XMLHttpReques ...

  5. Vagrant入门[转]

    Vagrant是一个简单易用的部署工具,用英文说应该是orchestration tool.它能帮助开发人员迅速的构建一个开发环境,帮助测试人员构建测试环境. Vagrant的基本工作原理大致如下: ...

  6. Python进阶之路---1.2python版本差异

    Python2.*与python3.*版本差异 作为一个初学者,我们应该如何选择python的版本进行学习呢,这两个版本有什么区别呢,接下来让我们简单了解一下,以便我们后续的学习. Python版本差 ...

  7. (转)简易WCF负载均衡方案

    最近跟高老师讨论nginx跟tomcat集群做负载均衡方案.感觉很有意思.想到自己项目中服务用的WCF技术,于是就想WCF如何做负载均衡,Google了一会,发现wcf4.0的路由服务好像可以实现.不 ...

  8. java HashMap的原理

    HashMap的数据结构: 在java编程语言中,最基本的结构就是两种,一个是数组,另外一个是模拟指针(引用),所有的数据结构都可以用这两个基本结构来构造的,HashMap也不例外.HashMap实际 ...

  9. redis基础操作

    /** * redis的Java客户端Jedis测试验证 * * @author */ public class Test { /** * 非切片客户端链接 */ private Jedis jedi ...

  10. poj3122 binary search 实数区间

    Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14536   Accepted: 4979   Special Ju ...