有关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;
}

}
...全文
146 4 打赏 收藏 转发到动态 举报
写回复
用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]
随着通信电子技术的迅速发展,信息技术给家居行业产生了深远的影响,家居环境的智能化监控已经成为智能家居的一个重要的发展方向。人们逐渐对自己的生活提出一种更高的要求,他们需要一种智能化、可交互,并且融合现代创新科技的产品来改善他们的生活环境,使他们生活更加安全、舒适、便捷、智能。本文根据智能家居的发展背景和研究现状,并且从实用性和可行性角度出发,研究设计了一种基于STM32单片机的智能家居系统。该系统由一个多功能综合的技术系统组成,各个多功能子系统间具有协同配合能力。基于STM32单片机实现的功能子系统包括:智能温度检测,智能湿度检测,智能烟雾/火灾检测智能检测,无线传输,人机交互机构,风扇调节,报警模块。 整个系统分为前端51单片机采集板和后端STM32单片机接收板。 采集板使用DHT11温湿度传感器、MQ烟雾传感器完成室内家居环境的采集。然后通过nRFL24L01将采集到的数据发送给后端。接收板使用LCD1602完成数据显示、使用蜂鸣器模块报警,使用风扇驱动模块调节室内家居环境。本文完成了智能家居控制系统前端、后端软硬件的设计,使用Altium designer绘制了电路原理图,使用Keil C完成了51单片机和STM32单片机的编程与调试。 最后,对本文设计的基于STM32的智能家居控制系统进行部署调试。试验结果表明,该系统可成功应用在智能家居环境检测调节和火灾安全防护的领域,可提高家居生活智能化水平。

65,210

社区成员

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

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