POJ-2492 A Bug's Life 并查集
2014-04-13 16:47
495 查看
题目链接
POJ1182 1703 类型
题目大意:给出N条虫子,然后a和b交配,给出M对a和b后问
有没有同性恋的虫子
POJ1182 1703 类型
题目大意:给出N条虫子,然后a和b交配,给出M对a和b后问
有没有同性恋的虫子
#include<stdio.h> #include<iostream> #include<string> #include<string.h> #include<math.h> #include<functional> #include<algorithm> #include<vector> #include<queue> using namespace std; const int maxn = 1000005; const int inf = 1<<29; int n,m; int p[maxn],sex[maxn]; //g 0表示相同 1表示不同 int find( int x ) { if( p[x] == x ) return x; int tmp = p[x]; p[x] = find(p[x]); sex[x] = (sex[x]+sex[tmp])%2; return p[x]; } void merge( int x,int y ) { int fx = find(x); int fy = find(y); p[fy] = fx; sex[fy] = ( sex[y] + sex[x] + 1 )%2; } void init() { for( int i = 0; i <= n; i ++ ){ p[i] = i; sex[i] = 0; } } int main() { #ifndef ONLINE_JUDGE freopen("data.txt","r",stdin); #endif char ch; int cas,x,y; scanf("%d",&cas); for( int cs = 1; cs <= cas; cs ++ ){ int flag = 0; scanf("%d%d",&n,&m); init(); for( int i = 0; i < m; i ++ ){ scanf("%d%d",&x,&y); if( find(x) == find(y) ){ if( sex[x] == sex[y] ) flag = 1; } else{ merge( x,y ); } } printf("Scenario #%d:\n",cs); if( flag ) puts("Suspicious bugs found!"); else puts("No suspicious bugs found!"); puts(""); } return 0; }
相关文章推荐
- 包含多集合的json反序列化
- JS事件
- javascript通过url向jsp页面传递中文参数乱码解决方法
- mysql 更新 语句中 的 safe_mode
- javaScript-console对象
- href="#"与href="javascript:void(0)"的区别
- jquery获取css color 值返回RGB
- Jsp与Servlet之间的传值
- JavaScript中冒号(:)
- js-DOM[1] 绑定~
- JSP页面间传递参数
- HTML Images
- ExtJS 4.0 的改变--较为完整的介绍
- 深入理解JavaScript系列(47):对象创建模式(上篇)
- html5学习canvas基本使用:div在js前面;localStorage存储根据浏览器在不同的位置;应用程序缓存;placeholder 属性提供一种提示(hint),描述输入域所期待的值。
- javascript学习之类型检测
- div + css + Js 实现 select 多选列表 完整实例
- jQuery1.11源码分析(10)-----Callbacks模块
- css
- sharedPreferences