您的位置:首页 > Web前端

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;
}
有时还是要把卡住的地方好好想想。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: