POJ1469COURSES(二分图匹配)
裸的二分图匹配
不解释:
1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <vector> 8 #include <cstdio> 9 #include <cctype> 10 #include <cstring> 11 #include <cstdlib> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 #define eps 1e-15 16 #define MAXN 305 17 #define INF 1000000007 18 #define MAX(a,b) (a > b ? a : b) 19 #define MIN(a,b) (a < b ? a : b) 20 #define mem(a) memset(a,0,sizeof(a)) 21 22 bool G[MAXN][MAXN],vis[MAXN]; 23 int Left[MAXN],N,P,T; 24 25 bool DFS(int u) 26 { 27 for(int v=0;v<=N;v++) if(G[u][v] && !vis[v]) 28 { 29 vis[v] = true; 30 if(!Left[v] || DFS(Left[v])) 31 { 32 Left[v] = u; 33 return true; 34 } 35 } 36 return false; 37 } 38 39 int main() 40 { 41 while(~scanf("%d", &T))while(T--) 42 { 43 mem(G); mem(Left); 44 scanf("%d%d", &P, &N); 45 int num,x; 46 for(int i=1;i<=P;i++) 47 { 48 scanf("%d",&num); 49 for(int j=0;j<num;j++) 50 { 51 scanf("%d", &x); 52 G[i][x] = true; 53 } 54 } 55 int ans = 1; 56 for(int i=1;i<=P;i++) 57 { 58 mem(vis); 59 if(!DFS(i)) {ans =0;break;} 60 } 61 printf("%s\n",ans?"YES":"NO"); 62 } 63 return 0; 64 }