HDU 1829 A Bug's Life
2015-06-16 17:20
211 查看
[align=left]Problem Description[/align]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 easyto 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. [align=left]Input[/align]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. [align=left]Output[/align]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 assumptionabout the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong. [align=left]Sample Input[/align]23 31 22 31 34 21 23 4 [align=left]Sample Output[/align]Scenario #1:Suspicious bugs found!Scenario #2:No suspicious bugs found!HintHuge input,scanf is recommended.题意:通过给出的小虫的关系(性别不同),判断是否存在小虫是同性恋。这题我写得还挺心酸的,刚开始想的是把所以同性的小虫合并在一个集合里,最后只要判断两个小虫的根节点是否一样,是则存在同性恋。但是我被将同性恋小虫合并卡住了。。。然后各种胡思乱想。。错误想法,供mark思考历程=。=最后用两个集合(数组)a,b来表示两个性别,初始化化为0,若该集合存在某只虫,则设置为1.接下来我用了几个if语句来判断。如果其中一个集合都找到当前两只虫,则有同性恋;如果其中一只虫其中一个集合(a或b)找到,而另一只虫不在两个集合中,则把该虫放到与前一只虫对立的一个集合(若在a则放入b),如果两只虫都不在两个集合中,则任意的把两只虫分别放到两个集合中。错误代码:
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <set> #include <algorithm> #include <queue> #include <stack> #define MAXN 30005 #define MAX 2005 #define INF 0x3f3f3f3f using namespace std; int flag,flag1,cnt = 0,t,p,q,n,m; int a[MAX],b[MAX]; int main() { scanf("%d",&t); while (t--) { cnt++; flag = 1; flag1 = 1; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); scanf("%d%d",&n,&m); for(int i = 0; i < m; i++) { scanf("%d%d",&p,&q); if(flag) { a[p] = 1; b[q] = 1; flag = 0; } if((a[p] == 1 && a[q] == 1 )|| (b[p] == 1 && b[q] == 1))//在一个集合找到 { flag1 = 0; } if( a[p] == 0 && a[q] == 1)//p不在v1,q在v1 { b[p] = 1; } if(a[p] == 1 && a[q] == 0)//p在v1,q不在v1 { b[q] = 1; } if(b[p] == 0 && b[q] == 1)//p不在v2,q在v2 { a[p] = 1; } if(b[p] == 1 && b[q] == 0)//p在v2,q不在v2 { a[q] =1; } if(a[p]== 0 && a[q] == 0 && b[p]== 0 && b[q] == 0) { a[p] = 1; b[q] = 1; } } printf("Scenario #%d:\n",cnt); if(flag1) { printf("No suspicious bugs found!\n"); } else { printf("Suspicious bugs found!\n"); } } return 0; }后来我才发现存在的问题是:比如样例是: 1 2 3 4 2 4 因为存在当两只虫都不在两个集合中时两只小虫是任意把到两个集合中的,所以,在上面样例中,3 和4 中的4可能会放到跟2一组,也可能会放到跟1一组,那么这答案就不一样了。。。打脸正确思路:其实我刚开始想的是对的,就是卡在将同性恋的合并。我们用一个数组sex[]来存放于当前的小虫异性的虫,那么下次将跟这只虫有关系的虫与它对立的虫合并就可以了。正确代码:
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <set> #include <algorithm> #include <queue> #include <stack> #define MAXN 30005 #define MAX 2005 #define INF 0x3f3f3f3f using namespace std; int flag1,cnt = 0,t,p,q,n,m; int Father[MAX]; int sex[MAX]; int Find(int x) { if(x != Father[x]) { Father[x] = Find(Father[x]); } return Father[x]; } void Merge(int x, int y) { int ix = Find(x); int iy = Find(y); if(ix != iy) { Father[iy] = ix; } } int main() { scanf("%d",&t); while (t--) { cnt++; flag1 = 1; scanf("%d%d",&n,&m); for(int i = 1; i <= n; i++) { Father[i] = i; sex[i] = 0; } while (m--) { scanf("%d%d",&p,&q); int x = Find(p); int y = Find(q); if(x == y) { flag1 = 0; continue; }
//关键代码 if(sex[p] == 0) { sex[p] = q;//设置为对立小虫 } else { Merge(sex[p],q);//将同性小虫合并 } if(sex[q] ==0) { sex[q] = p; } else { Merge(sex[q],p); } } printf("Scenario #%d:\n",cnt); if(flag1) { printf("No suspicious bugs found!\n\n"); } else { printf("Suspicious bugs found!\n\n"); } } return 0; }
有时还是要把卡住的地方好好想想。。。
相关文章推荐
- Jquery easyui Tree的简单使用
- 上传文件以中文方式命名,放入html标签会报错
- CSS遮罩层,全兼容
- 向WebBrowser控件插入HTML代码
- html 垂直对齐
- 【Extjs】 checkboxmodel 中事件处理
- js中(function(){…})()立即执行函数写法理解
- JQ怎么获取margin-left的值
- SBJson的使用
- HTML传输方法(只有Post和Get)
- JS中将时间戳转格式
- 【SHARE】WEB前端学习资料
- 伪元素改变date类型input框的默认样式实例页面
- 常用js函数--个人项目总结
- Count Complete Tree Nodes
- javascript设置canvas宽高
- Atitit.js获取上传文件全路径
- Atitit.js获取上传文件全路径
- 【转】WEB前端调优
- CSS布局奇淫技巧之--各种居中