有关bug's life的问题

pvagabond 2011-08-13 04:04:01
这个是POJ 2492 A bug's life:

我写了如下的代码,自己感觉还满正确的,但是却总是通过不了,不知道为什么,求各位大牛能不能帮忙一下。。在下感激涕零。。

#include <iostream>
using namespace std;

const int UP_LIMIT = 2001;
int num[UP_LIMIT] = {0};
int prev[UP_LIMIT] = {0};
int opp[UP_LIMIT] = {0};

void Make_Set(int x)
{
num[x] = 1;
prev[x] = x;
}

int Find_Set(int x)
{
if(x != prev[x])
prev[x] = Find_Set(prev[x]);
return prev[x];
}

void Union_Set(int x, int y)
{
x = Find_Set(x);
y = Find_Set(y);
if(x != y)
{
num[x] += num[y];
prev[y] = x;
}
}

void Make_Opposite(int x, int y)
{
if(opp[x] != 0)
Union_Set(y,opp[x]);
if(opp[y] != 0)
Union_Set(x,opp[y]);
opp[x] = Find_Set(y);
opp[y] = Find_Set(x);
}

int main()
{
int n, m; //n: # of bugs, m: # of interactions
int l = 0, tempA, tempB = 0, count = 0, button =0;
cin>>count;
while(l++<count)
{
button = 0; //reset
cin>>n>>m;
//make sets
for(int i=1;i<=n;i++)
Make_Set(i); // summing up to n sets
for(int i=0;i<m;i++)
{
cin>>tempA>>tempB;
if(Find_Set(tempA) != Find_Set(tempB))
Make_Opposite(tempA, tempB);
else
button = 1;
}
cout<<"Scenario #"<<l<<":"<<endl;
if(button)
cout<<"Suspicious bugs found!"<<endl<<endl;
else
cout<<"No suspicious bugs found!"<<endl<<endl;
}

}
...全文
84 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
至善者善之敌 2011-08-13
  • 打赏
  • 举报
回复
for(int j=0;j<m;j++) //这里换成j试试
{
cin>>tempA>>tempB;
if(Find_Set(tempA) != Find_Set(tempB))
Make_Opposite(tempA, tempB);
else
button = 1;
}
  • 打赏
  • 举报
回复
看看。。
pvagabond 2011-08-13
  • 打赏
  • 举报
回复
题目在此。http://poj.org/problem?id=2492

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.
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

2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
Sample Output

Scenario #1:
Suspicious bugs found!

Scenario #2:
No suspicious bugs found!
Hint

Huge input,scanf is recommended.
pvagabond 2011-08-13
  • 打赏
  • 举报
回复
i 和 j 没有任何区别吧, 而且我试了,也不行。。
[Quote=引用 3 楼 babilife 的回复:]

for(int j=0;j<m;j++) //这里换成j试试
{
cin>>tempA>>tempB;
if(Find_Set(tempA) != Find_Set(tempB))
Make_Opposite(tempA, tempB);
else
button = 1;
}
[/Quote]

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧