POJ-1469 COURSES 完备匹配
题意:给定一些学生和课程的关系,问是否每一门课程能唯一对应一个学生。
解法:每一条边对应一个选择关系,问题就是求一个完备匹配是否存在。
代码如下:
#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; int P, N; char G[105][305], vis[305]; int match[305]; bool path(int u) { for (int i = 1; i <= N; ++i) { if (!G[u][i] || vis[i]) continue; vis[i] = 1; if (!match[i] || path(match[i])) { match[i] = u; return true; } } return false; } bool query() { int ret = 0; memset(match, 0, sizeof (match)); for (int i = 1; i <= P; ++i) { memset(vis, 0, sizeof (vis)); ret += path(i); } return ret == P; } int main() { int T, num, c; scanf("%d", &T); while (T--) { memset(G, 0, sizeof (G)); scanf("%d %d", &P, &N); for (int i = 1; i <= P; ++i) { scanf("%d", &num); for (int j = 1; j <= num; ++j) { scanf("%d", &c); G[i][c] = 1; } } printf(query() ? "YES\n" : "NO\n"); } return 0; }