poj-2492 -A Bug's Life-并查集的使用
来源:互联网 发布:java读取ini配置文件 编辑:程序博客网 时间:2024/06/15 23:44
题目意思:
给出N条虫子,然后a和b交配,给出M对a和b后问有没有同性恋的虫子
思路:
并查集:
把相同性别的虫子连到一起,看是否有矛盾
CODE
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<stack>using namespace std;int ls[20010];int f[20010];int fid(int x){ return x == f[x] ?x:fid(f[x]);}void bc(int x,int y){ int xx = fid(x); int yy = fid(y); if(xx!=yy) f[xx] = yy;}int main(){ int t; scanf("%d",&t); int ki = 1; while(t--) { int n, m; scanf("%d%d",&n,&m); memset(ls,0,sizeof(ls)); for(int i = 0; i <= n; i ++) f[i] = i; int flag = 0; for(int i = 1; i <= m; i++) { int a, b; scanf("%d%d",&a,&b); if(!ls[a]&&!ls[b]) { ls[a] = b; ls[b] = a; } else if(!ls[a]&&ls[b]) { ls[a] = b; bc(a,ls[b]); } else if(ls[a]&&!ls[b]) { ls[b] = a; bc(b,ls[a]); } else { bc(ls[a],b); bc(ls[b],a); } if(fid(a) == fid(b)) flag = 1; } printf("Scenario #%d:\n",ki++); if(flag) { printf("Suspicious bugs found!\n"); } else { printf("No suspicious bugs found!\n"); } printf("\n"); } return 0;}
0 0
- poj-2492 -A Bug's Life-并查集的使用
- poj 2492 A Bug's Life(并查集)
- POJ 2492 A Bug's Life(并查集)
- poj 2492 并查集 A Bug's Life
- POJ-2492 a bug's life 并查集
- poj 2492 并查集 a bug's life
- POJ 2492 A Bug's Life (并查集)
- poj 2492 A Bug's Life(种类并查集)
- poj 2492 -- A Bug's Life(并查集)
- POJ 2492 A Bug's Life (并查集)
- poj 2492 - A Bug's Life(并查集)
- 【种类并查集】-POJ-2492-A bug's life
- POJ 2492 A Bug's Life(并查集)
- POJ-2492 A Bug's Life 并查集
- [POJ 2492] A Bug's Life [扩展并查集]
- poj 2492 A Bug's Life 【并查集拓展】
- poj 并查集 - 2492 A Bug's Life
- POJ 2492 A Bug's Life 并查集
- 哲学家就餐问题——and型信号量机制
- 动画缓冲
- linux--分卷压缩解压缩
- JavaScript如何在离开页面时提示用户
- vim命令总结
- poj-2492 -A Bug's Life-并查集的使用
- xcode7、iOS9 设置启动图片(Launch Image)
- perl - 单引号和双引号字符串
- 特别码字
- 【小白装系统】——BIOS简介
- 栈的知识
- 待更正
- 拒绝OOM,打造自定义帧动画
- javascript 简单导航菜单设计