A Bug's Life POJ - 2492(一题多解,二分图思路)

今天看《挑战》看到一种新的算法,二分图染色问题,然后突然想起之前做的一道带权并查集问题详细见链接

二分图问题是给定一个图判断能否将其染色保证任意两相连点的颜色不一样(只能染两种颜色~)

代码

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

const int maxn = 2200;

vector<int> G[maxn];
int color[maxn];

int n, m;

bool dfs(int u, int c) {
    color[u] = c;
    for(int i = 0; i < G[u].size(); i++) {
        if (color[G[u][i]] == c) return false;
        if (color[G[u][i]] == 0 && !dfs(G[u][i], -c)) return false;
    }
    return true;
}

void solve() {
    for(int i = 1; i <= n; i++) {
        if (color[i] == 0) {
            if (!dfs(i, 1)) {
                printf("Suspicious bugs found!\n\n");
                return;
            }
        }
    }
    printf("No suspicious bugs found!\n\n");
}

int main() {
    // freopen("input.txt", "r", stdin);
    int T;
    int u, v;
    int kase = 0;
    scanf("%d", &T);
    while(T--) {
        memset(color, 0, sizeof(color));
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; i++) {
            G[i].clear();
        }
        while(m--) {
            scanf("%d%d", &u, &v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
        printf("Scenario #%d:\n", ++kase);
        solve();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值