avatar
fireworks99
keep hungry keep foolish

POJ 2492 A Bug's Life(easy union-find-set)

Description

这题跟POJ 1703一样,没什么好说的。写题解是提醒自己unite函数里,连接前if判断的重要性,我今天尝试将unite在主函数里实现,忘记了if判断(两者在同一集合里还乱连接)直接连接导致RE

Code

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

bool flag;
int n, m, a, aa, b, bb, pre[10005];

int found(int x)
{
    if(pre[x] == -1)
        return x;
    return pre[x] = found(pre[x]);
}

int main()
{
    int _, cnt = 1;
    scanf("%d", &_);
    while(_--)
    {
        flag = 0;
        memset(pre, -1, sizeof(pre));
        scanf("%d %d", &n, &m);
        while(m--)
        {
            scanf("%d %d", &a, &b);
            if(flag)
                continue;
            aa = found(a);
            bb = found(b);
            if(aa != bb)
            {
                int bbnn = found(b + n), aann = found(a + n);
                if(aa != bbnn && bb != aann)
                    pre[aa] = found(b + n), pre[bb] = found(a + n);
            }
            else
                flag = 1;
        }
        cout << "Scenario #" << cnt++ << ":\n";
        if(flag)
            cout << "Suspicious bugs found!" << '\n' << '\n';
        else
            cout << "No suspicious bugs found!" << '\n' << '\n';
    }
    return 0;
}
Site by Baole Zhao | Powered by Hexo | theme PreciousJoy