【HDU2120】Ice_cream's world I(并查集基础题)
查环操作,裸题。一次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(并查集基础题)的更多相关文章
- 【HDU1231】How Many Tables(并查集基础题)
什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...
- 【HDU1232】畅通工程(并查集基础题)
裸敲并查集,很水一次AC #include <iostream> #include <cstring> #include <cstdlib> #include &l ...
- 【HDU1856】More is better(并查集基础题)
裸并查集,但有二坑: 1.需要路径压缩,不写的话会TLE 2.根据题目大意,如果0组男孩合作的话,应该最大的子集元素数目为1.所以res初始化为1即可. #include <iostream&g ...
- 【HDU1272】小希的迷宫(并查集基础题)
仍旧裸敲并查集.有这两点注意: 1.输入 0 0 时候要输出YES 2.留心数组的初始化 #include <iostream> #include <cstring> #inc ...
- 【HDU1325】Is It A Tree?(并查集基础题)
有以下坑点: 1.结束输入不一定-1,题目中的叙述只是说所有权值都为正值. 2.是否构成一棵树不能只判断是否只有一个根节点,没有环路,而且还需要判断每个节点的入度一定是1,不然就不是一棵树. (无环路 ...
- poj1182 食物链(并查集 好题)
https://vjudge.net/problem/POJ-1182 并查集经典题 对于每只动物创建3个元素,x, x+N, x+2*N(分别表示x属于A类,B类和C类). 把两个元素放在一个组代表 ...
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...
- Brain Network (easy)(并查集水题)
G - Brain Network (easy) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- PAT甲级 并查集 相关题_C++题解
并查集 PAT (Advanced Level) Practice 并查集 相关题 <算法笔记> 重点摘要 1034 Head of a Gang (30) 1107 Social Clu ...
随机推荐
- 【HDU】I love sneakers!(分组背包)
看了许多的题解,都有题目翻译,很不错,以后我也这样写.直接翻译样例: /*鞋子的数量N[1, 100]; 拥有的金钱M[1, 1w]; 品牌数目[1, 10]*/ /*以下四行是对于每双鞋的描述*/ ...
- 无Root抓包:Packet Capture 1.2.3
无Root抓包:Packet Capture 1.2.3 http://www.coolapk.com/apk/app.greyshirts.sslcapture
- (转)A drop-in universal solution for moving text fields out of the way of the keyboard
There are a hundred and one proposed solutions out there for how to move UITextField andUITextView o ...
- Mac OS X 下修改网卡地址和抵御 ARP 攻击
用 Mac 系统有一段时间了,这里记录一下自己遇到的需要终端命令解决的问题. 网络环境绑定了原先机器的 MAC 地址,由于特殊原因,先把新机器的网卡地址改成原先那台. 在终端输入sudo ifconf ...
- jQuery限制文本框只能输入正整数
//限制键盘只能按数字键.小键盘数字键.退格键 $("#txtQty").keydown(function (e) { var code = parseInt(e.keyCode) ...
- 菜鸟学EJB(二)——在同一个SessionBean中使用@Remote和@Local
不废话.直接进入正题: 在Jboss4及曾经的版本号中,例如以下代码能够成功部署: package com.tjb.ejb; import javax.ejb.Local; import javax. ...
- 漫话Unity3D(一)
前言 使用Unity已经有将近半年时间了,尽管项目还仅仅是个半成品,可是Unity差点儿相同玩熟了.这里把使用过程中碰到的问题梳理一遍.不会涉及到太过详细的功能和代码,可是假设开发一个网游又都会涉及到 ...
- RMAN的show,list,crosscheck,delete命令
1.SHOW命令: 显示rman配置: RMAN> show all; 2.REPORT命令: 2.1.RMAN> report schema 报告目标数据库的物理结构; 2.2 ...
- (转)CommandArgument用法
1.绑定数据库中一个主键前台代码:<ItemTemplate> <asp:ImageButton ID="ibtnUpdate ...
- Ado.net对象
Connection对象主要提供与数据库的连接功能 Command 对象用于返回数据,修改数据,运行存储过程以及发送或检索参数信息的数据库命令. DataReader对象通过Command对象提供从数 ...