[POJ 1422] Air Raid
[题目链接]
http://poj.org/problem?id=1422
[算法]
二分图最小路径点覆盖
[代码]
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; #define MAXN 210 int i,n,m,u,v,T,ans; int match[MAXN]; bool g[MAXN][MAXN]; bool visited[MAXN]; inline bool hungary(int u) { int v; for (v = 1; v <= n; v++) { if (g[u][v] && !visited[v]) { visited[v] = true; if (!match[v] || hungary(match[v])) { match[v] = u; return true; } } } return false; } int main() { scanf("%d",&T); while (T--) { memset(g,false,sizeof(g)); memset(match,0,sizeof(match)); scanf("%d%d",&n,&m); for (i = 1; i <= m; i++) { scanf("%d%d",&u,&v); g[u][v] = true; } ans = 0; for (i = 1; i <= n; i++) { memset(visited,false,sizeof(visited)); if (hungary(i)) ans++; } printf("%d\n",n - ans); } return 0; }