有关bug's life的问题
这个是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;
}
}