Crime

Crime

这里写图片描述

题意:给出n个点m条边,每条边有且只能选一个点,问最少选多少个点
.
.
题解:经典的黑白染色,dfs一次就可以了

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int maxn = 101000;
const int maxm = 100100;
int a[2*maxm], nextt[2*maxm], last[maxn], n, m, tot, t, sum;
int f[maxn];
bool flag;

int min(int x, int y) {
    if (x<y) return x;
    return y;
}

void insert(int x, int y) {
    tot++;
    a[tot] = y;
    nextt[tot] = last[x];
    last[x] = tot;
}

void dfs(int x) {
    sum++;
    if (f[x] == 1) t++;
    int k = last[x];
    while (k != 0) {
        if (f[a[k]] == 0) {
            f[a[k]] = -f[x];
            dfs(a[k]);
        } else if (f[a[k]] == f[x]) {
            flag = false;
            return;
        }
        if (!flag) return;
        k = nextt[k];
    }
}

int main() {


    while (scanf("%d %d", &n, &m) != EOF) {
        memset(last, 0, sizeof(last));
        memset(a, 0, sizeof(a));
        memset(nextt, 0, sizeof(nextt));
        memset(f, 0, sizeof(f));
        tot = 0;
        flag = true;
        int x, y, ans = 0;
        for (int i = 1; i <= m; i++) {
            cin >> x >> y;
            insert(x, y);
            insert(y, x);
        }
        for (int i = 1; i <= n; i++) if (f[i] == 0) {
            sum = 0;
            t = 0;
            f[i] = 1;
            dfs(i);
            ans += min(sum-t, t);
        }
        if (!flag) {
            cout << "Impossible" << endl;
        } else 
        cout << ans << endl;
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值