题目:问course是否有完美匹配
/************************************************ Author :DarkTong Created Time :2016/7/30 22:28:35 File Name :Poj_1469.cpp *************************************************/ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <cmath> #include <cstdlib> #include <ctime> #define INF 0x3f3f3f3f #define esp 1e-9 typedef long long LL; //using namespace std; #define min(a, b) a<b?a:b #define max(a, b) a>b?a:b const int maxn = 300 + 10; int W[maxn*3][maxn], n, m; int left[maxn]; bool used[maxn]; bool match(int i) { for(int j=1;j<=n;++j) if(W[j][i]&&!used[j]) { used[j] = true; if(!left[j]||match(left[j])) { left[j] = i; return true; } } return false; } int hungary() { int res=0; memset(left, 0, sizeof(left)); for(int i=1;i<=m;++i) { memset(used, 0, sizeof(used)); if(match(i)) res++; } return res; } int main() { int T; scanf("%d", &T); while(T--) { memset(W, 0, sizeof(W)); scanf("%d%d", &m, &n); for(int i=1;i<=m;++i) { int t, x; scanf("%d", &t); for(int j=0;j<t;++j) { scanf("%d", &x); W[x][i]=1; } } if(hungary()==m) puts("YES"); else puts("NO"); } return 0; }
时间: 2024-12-19 12:14:09