poj2492 A Bug's Life (并查集拓展)
来源:互联网 发布:库存软件免费版 编辑:程序博客网 时间:2024/05/16 11:23
C - A Bug's Life
Crawling in process...Crawling failedTime Limit:10000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uSubmit
Appoint description:
Description
Background
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.
Problem
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.
Problem
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.
Input
The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.
Output
The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.
Sample Input
23 31 22 31 34 21 23 4
Sample Output
Scenario #1:Suspicious bugs found!Scenario #2:No suspicious bugs found!
Hint
Huge input,scanf is recommended.
说起来并查集真是一个好东西
就 我目前知道的而言 1.判断是否成环2.计算环的个数3.计算集合的个数 其实都是一个道理 就是看你怎么想
当然了 并查集还有按秩合并 压缩路径
怎么解释按秩合并呢 其实说白了就是按照两个人的rank等级建立关系
int x=find(a); int y=find(b); if(rank[x]>rank[y])//按秩合并 fa[y]=x; else { fa[x]=y; if(rank[x]==rank[y]) rank[y]++; }压缩路径(当看到数组特别大时 慎重啊 爆栈 如hdu1272)
压缩路径的意思就是在查找的过程中 使当前节点的值更新,直到根节点 便于下次查找节省时间
int find(int x){if(fa[x]!=x) fa[x]=find(fa[x]);return fa[x];}
不压缩路径:
int find(int x) 13.{ 14. while(fa[x]!=x) 15. x=fa[x]; 16. return fa[x]; 17.}
这道题起初真的没理解啊。。直接并查集 结果错了 听到队友说好像要分男女 也想了一个方法 就是判断
这对关系中某个人有没有出现过 一个出现另外一个就好判断了 后来想了一组测试数组 1 2 3 4 1 3 。。。我想错了
最后看的别人的代码 理解了
思路: 我们可以把男 女分为两个集合 如果出现的新的关系里面并查集出现了环 那么 就说明这两个人都是男 或者都是女
#include <stdio.h>#include <string.h>int fa[2005];int rela[2005]; bool bugs;void init(int n){for(int i=1;i<=n;i++)fa[i]=i,rela[i]=0;}int find(int x){if(fa[x]!=x) fa[x]=find(fa[x]);return fa[x];}void uni(int a,int b){int aa=find(a);int bb=find(b);if(aa!=bb)fa[aa]=bb;}int main(){int ncase;int t=1;scanf("%d",&ncase);while(ncase--){int n,m;scanf("%d %d",&n,&m);init(n);bugs=false;for(int i=0;i<m;i++){int x,y;scanf("%d %d",&x,&y);if(!bugs){int xx=find(x);int yy=find(y);if(xx==yy) bugs=true;if(rela[x]) uni(rela[x],y);else rela[x]=y;if(rela[y]) uni(rela[y],x);else rela[y]=x; }}printf("Scenario #%d:\n",t++);if(bugs)puts("Suspicious bugs found!\n");elseputs("No suspicious bugs found!\n");}return 0;}
1 0
- poj2492 A Bug's Life (并查集拓展)
- POJ2492--A Bug's Life--并查集
- poj2492 A Bug's Life 种类并查集
- POJ2492 A Bug's Life (并查集)
- poj2492--A Bug's Life(并查集变形)
- POJ2492 A Bug's Life 【并查集】
- poj2492 A Bug's Life(并查集)
- poj2492 A Bug's Life 并查集
- poj2492 A Bug's Life(并查集)
- POJ2492 A Bug's Life 并查集
- POJ2492 A Bug's Life 种群并查集
- poj2492 A Bug's Life(并查集)
- POJ2492 a bug's life并查集扩展
- POJ2492 -A Bug's Life(种类并查集)
- poj 2492 A Bug's Life 【并查集拓展】
- A Bug's Life(并查集拓展)
- poj2492——A Bug's Life(并查集扩展)
- poj2492 A bug's life{种类并查集}
- C#Winform技术,利用DataGridViewRow向DataGridView中添加数据
- linux下如何使用systemctl管理systemd服务与单元
- Sicily|1721. Gray code[Special judge]
- 区块链走进教育机构,开设课程并且运用于学位证书
- appwidget桌面小部件
- poj2492 A Bug's Life (并查集拓展)
- LeetCode 347. Top K Frequent Elements(最频繁的K个元素)
- 双击Eclipse,弹出一个错误窗口: java.lang.IllegalStateException:
- CodeForces 609C Load Balancing
- 处理机调度
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- POJ2104 —— K-th number
- Netty
- 四:资讯列表实现(借助PullToRefres实现上拉和下拉刷新)