Thursday, October 28, 2010

poj_2492_A_Bug_s_Life.cpp

Problem Links:


poj2492,

Problem:

A Bug's Life
Time Limit: 10000MS
Memory Limit: 65536K
Total Submissions: 16419
Accepted: 5275
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.
Source
TUD Programming Contest 2005, Darmstadt, Germany

Solution:


Disjoint set structure, find-union algorithms.

Source Code:

//Fri Jun  4 21:49:36 EDT 2010
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define MMAX 2001
using namespace std;

class Bugs
{
public:
    int parent; //The root;
    bool gender; //false, if it has the same gender with the root; otherwise, true;
};
vector<Bugs> v(MMAX);

void Makeset(int N)
{
    for (int i = 1; i <= N; i++)
    {
        v[i].parent = i;
        v[i].gender = false;
    }
}

int Find(int x)
{
    if (x == v[x].parent)
        return x;
    int temp = v[x].parent;
    v[x].parent = Find(v[x].parent);
    v[x].gender = v[temp].gender == false ? v[x].gender : !v[x].gender;
    return v[x].parent;
}

void Union(int t1, int t2, int x, int y)
{
    v[t2].parent = t1;
    v[t2].gender = v[x].gender == v[y].gender ? true : false;
}

int main(int argc, const char* argv[])
{
//  freopen("input.in", "r", stdin);
//  freopen("output.out", "w", stdout);
    int T;
    cin >> T;
    for (int ncase = 1; ncase <= T; ncase++)
    {
        int N, M;
        cin >> N >> M;
        Makeset(N);
        bool flag = true;
        for (int i = 0; i < M; i++)
        {
            int a, b;
            scanf("%d%d", &a, &b);
            int t1 = Find(a);
            int t2 = Find(b);
            if (t1 == t2 && v[a].gender == v[b].gender)
            {
                flag = false;
            }
            else
            {
                Union(t1, t2, a, b);
            }
        }
        cout << "Scenario #" << ncase << ":" << endl;
        if (flag == true)
            cout << "No suspicious bugs found!" << endl;
        else
            cout << "Suspicious bugs found!" << endl;
        cout << endl;
    }
//  fclose(stdin);
//  fclose(stdout);
    return 0;
}

2 comments :

Yi Ding said...

Can this be done with BFS ?

Yi Ding said...

My answer would be yes, but it's just too slow comparing with disjoint-set + find-union algorithm.
My implementation uses O(N*M).
If you uses BFS, then it will take O(N^2*M)