Thursday, October 28, 2010


Problem Links:



A Bug's Life
Time Limit: 10000MS
Memory Limit: 65536K
Total Submissions: 16419
Accepted: 5275
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.

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.
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.
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
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!
Huge input,scanf is recommended.
TUD Programming Contest 2005, Darmstadt, Germany


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
    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("", "r", stdin);
//  freopen("output.out", "w", stdout);
    int T;
    cin >> T;
    for (int ncase = 1; ncase <= T; ncase++)
        int N, M;
        cin >> N >> M;
        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;
                Union(t1, t2, a, b);
        cout << "Scenario #" << ncase << ":" << endl;
        if (flag == true)
            cout << "No suspicious bugs found!" << endl;
            cout << "Suspicious bugs found!" << endl;
        cout << endl;
//  fclose(stdin);
//  fclose(stdout);
    return 0;


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)