COURSES Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18892 Accepted: 7455
Description
Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exactly P students that satisfies simultaneously the conditions:Input
Your program should read sets of data from the std input. The first line of the input contains the number of the data sets. Each data set is presented in the following format:Output
The result of the program is on the standard output. For each input data set the program prints on a single line "YES" if it is possible to form a committee and "NO" otherwise. There should not be any leading blanks at the start of the line.Sample Input
2 3 3 3 1 2 3 2 1 2 1 1 3 3 2 1 3 2 1 3 1 1
Sample Output
YES NO
Source
Southeastern Europe 2000#include#include bool g[105][305]; int cx[105], cy[305]; bool vis[305]; int n, p; int DFS(int x) { for(int y = 1; y <= n; y++) { if(!vis[y] && g[x][y]) { vis[y] = true; if(cy[y] == -1 || DFS(cy[y])) { cx[x] = y; cy[y] = x; return 1; } } } return 0; } int MaxMatch() { int res = 0; memset(cx, -1, sizeof(cx)); memset(cy, -1, sizeof(cy)); for(int i = 1; i <= p; i++) { if(cx[i] == -1) { memset(vis, false, sizeof(vis)); res += DFS(i); } } return res; } int main() { int T; scanf("%d", &T); while(T--) { memset(g, false, sizeof(g)); scanf("%d %d", &p, &n); for(int i = 1; i <= p; i++) { int cnt; scanf("%d", &cnt); while(cnt --) { int j; scanf("%d", &j); g[i][j] = true; } } int ans = MaxMatch(); if(ans == p) printf("YES\n"); else printf("NO\n"); } }