Try Again

POJ 2492 A Bug's Life

A Bug's Life
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 38229   Accepted: 12436

Description

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!

带权并查集
#include <iostream> 
#include <algorithm> 
#include <cstring> 
#include <cstdio>
#include <vector> 
#include <queue> 
#include <cstdlib> 
#include <iomanip>
#include <cmath> 
#include <ctime> 
#include <map> 
#include <set> 
using namespace std; 
#define lowbit(x) (x&(-x)) 
#define max(x,y) (x>y?x:y) 
#define min(x,y) (x<y?x:y) 
#define MAX 100000000000000000 
#define MOD 1000000007
#define pi acos(-1.0) 
#define ei exp(1) 
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(false)
#define INF 0x3f3f3f3f3f 
#define mem(a) (memset(a,0,sizeof(a))) 
typedef long long ll;
long long a[2005],vis[2005],ans=0,n,m,x,y;
long long find(long long x)
{
  if(x==a[x]) return x;
  long long t=a[x];
  a[x]=find(a[x]);
  vis[x]=(vis[x]+vis[t]+1)%2;
  return a[x];
}
void uni(long long x,long long y)
{
  long long xx,yy;
  xx=find(x);
  yy=find(y);
  a[xx]=yy;
  vis[xx]=(vis[y]-vis[x])%2;
}
int main()
{
  int T;
  scanf("%d",&T);
  int t=T;
  while(T--)
  {
    ans=0;
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++)
    {
      a[i]=i;
      vis[i]=1;
    }
    while(m--)
    {
      scanf("%lld%lld",&x,&y);
      if(find(x)==find(y))
      {
        if(vis[x]==vis[y]) ans=1;
      }
      else uni(x,y);
    }
    printf("Scenario #%d:\n",t-T);
    if(ans) printf("Suspicious bugs found!\n\n");
    else printf("No suspicious bugs found!\n\n");
  }
  return 0;
}

 

posted @ 2017-08-01 16:10  十年换你一句好久不见  阅读(131)  评论(0编辑  收藏  举报