HDU 1829/POJ 2492 A Bug's Life

本文针对ABug'sLife编程问题提供了解决方案,通过并查集算法判断虫子互动是否符合异性相吸的原则。输入包括实验中虫子的数量及互动情况,输出则表明是否存在违背该原则的现象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A Bug's Life

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11981    Accepted Submission(s): 3901


Problem 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!
Hint
Huge input,scanf is recommended.
 
并查集题目。
记录一下当前结点到根结点的距离。 当再次找进行”并“操作时,判断属于同一根节点的两个节点到根节点的距离,同时为奇数或偶数,那么久存在矛盾。
  
/* ***********************************************
Author        :pk28
Created Time  :2015/8/15 9:54:22
File Name     :4.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 3000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

bool cmp(int a,int b){
    return a>b;
}
int fa[maxn];
int d[maxn];
int n,m,mark;
void init(){
    for(int i=1;i<=n+5;i++){
        d[i]=0;
        fa[i]=i;
    }
    mark=0;
}
int findfa(int x){
    if(x==fa[x])return x;
    else{
        int root=findfa(fa[x]);
        d[x]+=d[fa[x]];//记录到根节点的距离
        fa[x]=root;
        return fa[x];
    }
}
void Union(int a,int b){
    int x=findfa(a);
    int y=findfa(b);
    if(x==y){
        if((d[a]&1)==(d[b]&1))mark=1;
        return ;
    }
    else{
        fa[x]=y;
        d[x]=(1+d[b]-d[a]);//向量
    }
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    int T;
    cin>>T;
    int a,b;
    int cnt=1;
    while(T--){
        scanf("%d%d",&n,&m);
        init();
        for(int i=0;i<m;i++){
            scanf("%d %d",&a,&b);
            if(mark)continue;
            Union(a,b);
        }
        printf("Scenario #%d:\n",cnt++);
        if(mark)printf("Suspicious bugs found!\n");
        else printf("No suspicious bugs found!\n");
        printf("\n");
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/pk28/p/4732059.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值