POJ 1469 COURSES "Two-point match"

Source: Internet
Author: User

COURSES
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 18438 Accepted: 7262

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 was possible to form a committee of exactly P students that satisfies simultaneously t He conditions:

    • Every student in the Committee represents a different course (a student can represent a course if he/she visits that cours E
    • Each course have a representative in the Committee

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 was presented in the following format:

P N
Count1 Student1 1 Student1 2 ... Student1 Count1
Count2 Student2 1 Student2 2 ... Student2 Count2
...
COUNTP STUDENTP 1 studentp 2 ... STUDENTP COUNTP

The first line, each data set contains, positive integers separated by one blank:p (1 <= P <=)-The Numbe R of courses and N (1 <= n <=)-the number of students. The next P lines describe in sequence of the courses? From Course 1 to course P, each line describing a course. The description of course I was a line this starts with an integer Count I (0 <= Count i <= N) representing the Numbe R of Students visiting course I. Next, after a blank, you basis l FIND the Count I students, visiting the course, each of the consecutive separated by one blank. Students is numbered with the positive integers from 1 to N.
There is no blank lines between consecutive sets of data. Input data is correct.

Output

The result of the program is in the standard output. For each input data set, prints on a, "YES" if it is possible to form a committee and "NO" otherwise . There should not being any leading blanks at the start of the line.

Sample Input

23 33 1 2 32 1 21 13 32 1 32 1 31 1

Sample Output

YESNO

Test instructions: There may be more than one student in each course that defines a student who can represent one of his favorite classes (and this class also represents the student), asking if there is a different student in each class to represent. It's actually a class that matches a student and asks if you can match all the classes.

。。。 two points;

Note: Using CIN will time out ...

Code:

#include <iostream> #include <cstdio> #include <queue> #include <cstring>const int M =310;using  Namespace Std;bool map[110][m], vis[m], Flag;int p, N, match[m];bool dfs (int u) {for (int i = 1; I <= n; + + i) {if (!vis[i] && Map[u][i]) {Vis[i] = 1;if (match[i] = = -1| | DFS (Match[i])) {Match[i] = U; return true;}}} return false;} int main () {int t;//cin >> t;scanf ("%d", &t), while (T--) {//cin >> P >> n;scanf ("%d%d", &p, &n ); memset (map, 0, sizeof (map)), memset (match,-1, sizeof (match)), int m, temp;flag = 1;for (int i = 1; I <= p; + + i) {//cin >> m;scanf ("%d", &m); if (M = = 0) flag = 0;while (M-) {//cin >> temp;scanf ("%d", &temp); Map[i][temp] = 1 ;}} if (flag = = 0) {cout << "no\n"; return 0;} else {int res = 0;for (int i = 1; I <= p; + + i) {memset (Vis, 0, sizeof (VIS)); if (Dfs (i)) res++;} if (res = = p) cout << "yes\n"; else cout << "no\n";}} return 0;}


POJ 1469 COURSES "binary match"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.