[洛谷P1330]封锁阳光大学
题目大意:一张无向图,问最少设置几个关键点使得有点被覆盖(一个关键点可以覆盖所有与它相连的点),关键点不可以相邻
题解:二分图染色,若不冲突则为较少的一种颜色数
卡点:无
C++ Code:
#include <cstdio> #include <algorithm> #include <cstdlib> #define maxn 10010 #define maxm 100010 int head[maxn], cnt; struct Edge { int to, nxt; } e[maxm << 1]; inline void addE(int a, int b) { e[++cnt] = (Edge) {b, head[a]}; head[a] = cnt; } int n, m, ans; int res[2]; int C[maxn]; void dfs(int u) { res[C[u]]++; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to; if (!~C[v]) { C[v] = C[u] ^ 1; dfs(v); } else if (C[v] != (C[u] ^ 1)) { puts("Impossible"); exit(0); } } } int main() { scanf("%d%d", &n, &m); for (int i = 0, a, b; i < m; i++) { scanf("%d%d", &a, &b); addE(a, b); addE(b, a); } __builtin_memset(C, -1, sizeof C); for (int i = 1; i <= n; i++) if (!~C[i]) { res[0] = res[1] = 0; C[i] = 1; dfs(i); ans += std::min(res[0], res[1]); } printf("%d\n", ans); return 0; }