HDU 2120 Ice_cream's world I
来源:互联网 发布:知柏地黄丸多少钱一瓶 编辑:程序博客网 时间:2024/05/04 17:37
并查集应用之 判断环:依次输入每条边,当边的两个点在同一个集合时,环数加一,否则合并。
#include<cstdio>#include<cstring>#include<iostream>#include<iomanip>#include<queue>#include<cmath>#include<stack>#include<map>#include<vector>#include<set>#include<algorithm>using namespace std;typedef long long LL;const int int_max = 0x07777777;const int int_min = 0x80000000;int n, m;int mymap [1100][1100];int root[1100];int find (int x){ while(x!=root[x]){ x = root[x]; } return x;}void combine (int x, int y){ int xx = find(x); int yy = find(y); if(xx!=yy){ root[yy] = xx; }}int main(int argc, const char * argv[]){ while(scanf("%d %d",&n, &m)!=EOF){ memset(mymap, 0, sizeof(mymap)); for(int i = 0; i < n; i++) root[i] = i; int result = 0; for(int i = 0; i < m; i++){ int x,y; cin >> x >> y; if(find(x)==find(y)){ result++; }else{ combine(x, y); } } cout << result << endl; }}
0 0
- hdu 2120 Ice_cream's world I
- HDU 2120 Ice_cream's world I
- hdu 2120 Ice_cream's world I
- hdu-2120-Ice_cream's world I
- hdu 2120 Ice_cream's world I
- hdu 2120 Ice_cream's world I
- HDU-2120-Ice_cream's world I
- HDU 2120 Ice_cream's world I
- hdu-2120 Ice_cream's world I
- hdu 2120 Ice_cream's world I
- hdu 2120 Ice_cream's world I
- HDU 2120 Ice_cream's world I
- HDU 2120 Ice_cream's world I
- hdu 2120 Ice_cream's world I
- hdu 2120 (Ice_cream's world I)
- 【HDU 2120 Ice_cream's world I】
- HDU 2120Ice_cream's world I
- HDU-2120 Ice_cream's world I
- MyISAM和Innodb比较整理总结
- java杂篇2
- Eclipse SVN插件的帐号、密码修改
- 1.2-逆序字符串 in-place
- CONVERT为日期转换函数,一般就是在时间类型
- HDU 2120 Ice_cream's world I
- 老BOJ 11 Counting
- UIAlertView的各种属性、方法及代理的使用
- JQuery上传插件Uploadify使用详解
- hdu 4902 Nice boat 多校第四场 线段树的区间置数+区间更新
- MVC精辟比喻
- VirtualBox 安装 Mac OS X 10.9
- mysql数据库锁定机制
- winfrom 截屏、抓屏