并查集 HDU 2120
来源:互联网 发布:java 2d游戏引擎 编辑:程序博客网 时间:2024/06/05 14:16
其实就是判断新加的点是否在同一个集合里面
代码虐我千百遍,我待代码入初恋
Ice_cream's world I
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 583 Accepted Submission(s): 333
Problem 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.
One answer one line.
Sample Input
8 100 11 21 32 43 40 55 66 73 64 7
Sample Output
3
#include <stdio.h>#include <stdlib.h>#include <malloc.h>#include <limits.h>#include <ctype.h>#include <string.h>#include <string>#include <queue>#include <algorithm>#include <iostream>#include <stack>#include <deque>#include <vector>#include <set>#include <map>using namespace std;#define MAXN 1000 + 10#define MAXN1 10000 + 10int father[MAXN];int a[MAXN1];int b[MAXN1];int find(int x){ if(x != father[x]){ father[x] = find(father[x]); } return father[x];}int main(){ int n,m; int i; while(~scanf("%d%d",&n,&m)){ for(i=0;i<MAXN;i++){ father[i] = i; } memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); for(i=0;i<m;i++){ scanf("%d%d",&a[i],&b[i]); } int sum = 0; for(i=0;i<m;i++){ int f1 = find(a[i]); int f2 = find(b[i]); if(f1 != f2){ if(f1 < f2){ father[f2] = f1; } else{ father[f1] = f2; } } else{ sum++; } } printf("%d\n",sum); } return 0;}
0 0
- 并查集 HDU 2120
- hdu 2120(并查集)
- hdu 2120 并查集+判环
- HDU 3371 并查集
- HDU 2473 并查集
- hdu 1116 并查集
- HDU 1856 并查集
- HDU 1598 并查集
- HDU-1232 并查集
- 【并查集】hdu 2818
- hdu 1232 并查集
- hdu 1856(并查集)
- HDU 1598 并查集
- HDU-1232(并查集)
- hdu 1856 并查集
- hdu 1829 并查集
- HDU-1272(并查集)
- hdu 1213 并查集
- 关于OPC
- POJ 2823 Sliding Window 单调队列
- 解决访问android官网和sdk不能下载问题
- 全局变量 出现undefined reference to
- 20141008个人日志
- 并查集 HDU 2120
- BZOJ 1146 CTSC 2008 网络管理 Network 树链剖分+二分答案+平衡树
- 比较web service和EJB
- python的__call__函数
- Android 通过mTitle修改标题 无效的解决办法
- 【内核研究】线程局部存储_TheadLocal
- HTTP协议:分块传输编码(Chunked transfer encoding)
- Dive into python第七章正则表达式学习笔记
- 调和级数