[Luogu] 封锁阳光大学
https://www.luogu.org/problemnew/show/P1330
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> using namespace std; const int maxn = ; int n, m; vector<int> g[maxn]; bool vis[maxn]; int a[maxn],sum[]; bool dfs(int u, int col) { vis[u] = true; a[u] = col; sum[col]++; for (int i = ; 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, - col)) return false; } } return true; } int main() { scanf("%d%d", &n, &m); for (int i = ; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); g[u].push_back(v); g[v].push_back(u); } int ans = ; for (int i = ; i <= n; i++) if (!vis[i]) { sum[] = sum[] = ; if (!dfs(i,)) { printf("Impossible\n"); return ; } ans += min(sum[], sum[]); } printf("%d\n", ans); return ; }