题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=2120

Ice_cream's world I

Description

ice_cream's world is a rich country, it has many fertile lands. Today, the queen of ice_cream wants award land to diligent ACMers. So there are some watchtowers are set up, and wall between watchtowers be build, in order to partition the ice_cream’s world. But how many ACMers at most can be awarded by the queen is a big problem. One wall-surrounded land must be given to only one ACMer and no walls are crossed, if you can help the queen solve this problem, you will be get a land.

Input

In the case, first two integers N, M (N<=1000, M<=10000) is represent the number of watchtower and the number of wall. The watchtower numbered from 0 to N-1. Next following M lines, every line contain two integers A, B mean between A and B has a wall(A and B are distinct). Terminate by end of file.

Output

Output the maximum number of ACMers who will be awarded.
One answer one line.

Sample Input

8 10
0 1
1 2
1 3
2 4
3 4
0 5
5 6
6 7
3 6
4 7

Sample Output

3

用并查集统计环的个数。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
using std::map;
using std::min;
using std::find;
using std::pair;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 1010;
const int INF = 0x3f3f3f3f;
struct UinonFind {
int ans, par[N], rank[N];
inline void init(int n) {
ans = 0;
rep(i, n + 1) {
par[i] = i;
rank[i] = 0;
}
}
inline int find(int x) {
while(x != par[x]) {
x = par[x] = par[par[x]];
}
return x;
}
inline void unite(int x, int y) {
x = find(x), y = find(y);
if(x == y) { ans++; return; }
if(rank[x] < rank[y]) {
par[x] = y;
} else {
par[y] = x;
rank[x] += rank[x] == rank[y];
}
}
inline void solve(int n, int m) {
init(n);
int x, y;
while(m--) {
scanf("%d %d", &x, &y);
unite(x, y);
}
printf("%d\n", ans);
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n, m;
while(~scanf("%d %d", &n, &m)) {
go.solve(n, m);
}
return 0;
}

hdu 2120 Ice_cream's world I的更多相关文章

  1. HDU 2120 Ice_cream&#39;s world I(并检查集合)

    职务地址:HDU 2120 这题尽管字数不多,但就是看不懂. . 意思是求最多有多少个被墙围起来的区域.显然就是求环的个数.然后用并查集求环个数就能够了. 代码例如以下: #include <i ...

  2. HDU 2120 Ice_cream's world I【并查集】

    解题思路:给出n对点的关系,求构成多少个环,如果对于点x和点y,它们本身就有一堵墙,即为它们本身就相连,如果find(x)=find(y),说明它们的根节点相同,它们之间肯定有直接或间接的相连,即形成 ...

  3. hdu 2121 Ice_cream’s world II

    Ice_cream’s world II http://acm.hdu.edu.cn/showproblem.php?pid=2121 Time Limit: 3000/1000 MS (Java/O ...

  4. HDU 2121 Ice_cream’s world II 最小树形图 模板

    开始学习最小树形图,模板题. Ice_cream’s world II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32 ...

  5. HDU 2121——Ice_cream’s world II——————【最小树形图、不定根】

    Ice_cream’s world II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  6. HDU 2121 Ice_cream’s world II 不定根最小树形图

    题目链接: 题目 Ice_cream's world II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  7. hdoj 2120 Ice_cream's world I【求成环数】

    Ice_cream's world I Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  8. HDOJ 2120 Ice_cream's world I

    Ice_cream's world I ice_cream's world is a rich country, it has many fertile lands. Today, the queen ...

  9. 杭电 2120 Ice_cream's world I (并查集求环数)

    Description ice_cream's world is a rich country, it has many fertile lands. Today, the queen of ice_ ...

随机推荐

  1. 慕课网-安卓工程师初养成-4-3 Java条件语句之多重 if

    来源:http://www.imooc.com/code/1355 多重 if 语句,在条件 1 不满足的情况下,才会进行条件 2 的判断:当前面的条件均不成立时,才会执行 else 块内的代码.例如 ...

  2. NTP服务器地址及IP

    yum install ntp */20 * * * * /usr/sbin/ntpdate 61.172.254.29 210.72.145.44 (国家授时中心服务器IP地址)133.100.11 ...

  3. Lamp源码包安装实录

    Lamp源码包安装实录 附件中是安装步骤,下载站点里包含视频(http://down.51cto.com/data/460776) 本文出自 "李晨光原创技术博客" 博客,请务必保 ...

  4. 网络设备模拟器 GNS3

    https://www.gns3.com/support/docs/linux-installation sudo dpkg --add-architecture i386 sudo add-apt- ...

  5. landsat8简介

    简介 2013年2月11号,NASA 成功发射了 Landsat 8 卫星,为走过了四十年辉煌岁月的 Landsat 计划重新注入新鲜血液.LandSat- 8上携带有两个主要载荷:OLI和TIRS. ...

  6. idea 14 svn安装

    安装SVN客户端. 打开IDEA 14 File-setting-Version coltorl-Subversion-General 填入安装路径 打开"VCS"菜单项然后点击& ...

  7. java 命令行操作

    打包命令 例如:将testproject下的文件打包成test.war jar cvf test.war ./testproject

  8. magento2 客户端模式less样式修改。

    后台模式改为客户端编译,修改less后要删除静态文件,然后清除缓存,清除浏览器缓存.刷新多次后生效.

  9. C#时间处理--DateTime和TimeSpan

    DateTime dt = DateTime.Now; dt.ToString();//2005-11-5 13:21:25 dt.ToFileTime().ToString();//12775641 ...

  10. 地球坐标系与投影方式的理解(关于北京54,西安80,WGS84;高斯,兰勃特,墨卡托投影)

    一.地球模型 地球是一个近似椭球体,测绘时用椭球模型逼近,这个模型叫做参考椭球,如下图: 赤道是一个半径为a的近似圆,任一圈经线是一个半径为b的近似圆.a称为椭球的长轴半径,b称为椭球的短轴半径. a ...