[Luogu] 封锁阳光大学

https://www.luogu.org/problemnew/show/P1330

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
const int maxn = 10010;
int n, m;
vector<int> g[maxn];
bool vis[maxn];
int a[maxn],sum[3];

bool dfs(int u, int col) {
    vis[u] = true;
    a[u] = col;
    sum[col]++;
    for (int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if (vis[v] && a[v] == a[u])
            return false;
        else if (!vis[v]) {
            if (!dfs(v, 3 - col))
                return false;
        }
    }
    return true;
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    int ans = 0;
    for (int i = 1; i <= n; i++)
        if (!vis[i]) {
            sum[1] = sum[2] = 0;
            if (!dfs(i,1)) {
                printf("Impossible\n");
                return 0;
            }
            ans += min(sum[1], sum[2]);
        }
    printf("%d\n", ans);

    return 0;
}

 

posted @ 2017-12-09 21:55  xayata  阅读(146)  评论(0编辑  收藏  举报