Courses(二分图水题)

Courses

Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status Practice HDU
1083

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:

. every student in the committee represents a different course (a student can represent a course if he/she visits that course)

. each course has a representative in the committee

Your program should read sets of data from a text file. The first line of the input file contains the number of the data sets. Each data set is 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 in each data set contains two positive integers separated by one blank: P (1 <= P <= 100) - the number of courses and N (1 <= N <= 300) - 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 is a line that starts with an integer Count i (0 <= Count i <= N) representing the number of students visiting course i. Next, after a blank, you‘ll find the Count i students, visiting the course,
each two consecutive separated by one blank. Students are numbered with the positive integers from 1 to N.

There are no blank lines between consecutive sets of data. Input data are correct.

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.

An example of program input and output:

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 

题意:有P门课,N个学生,求能否求出一个组满足:每门课只能对应一个人,但是单个人可以对应多门课 且人数(or课程)等于P。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

using namespace std;

int map[502][502];

bool visited[502]; //标记男生是否被访问
int match[502];    //女生和男生的匹配情况

int n,m;

bool find(int i)   //查找当前的i是否可以匹配
{
    int j;
    for(j=1;j<=n;j++)
    {
        if(map[i][j]&&!visited[j])//在这里很有必要说一下visited这个数组,因为在之后的递归中,就会把这个标记好了的对象默认为
			                      //已经和上一次的对象匹配过了,这样就不会再访问这个对象了,这在好几个的连续递归中显得尤为重要
        {
            visited[j]=1;
            if(match[j]==-1||find(match[j]))  //在此次查找的时候其实如果已经匹配过了则会在调用的时候即使是已经匹配成功了,由于
				                               //当时在匹配的时候已经对可以匹配成功的做了match的标记了,就会继续查找他的下一个
											   //能够匹配的对象
            {
                match[j]=i;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    int k,x,y,ans,tmp;
    int T;
    scanf("%d",&T);
    while(T--)
    {
        ans=0;
        memset(map,0,sizeof(map));
        memset(match,-1,sizeof(match));
        scanf("%d%d",&m,&n);
        for(int i=1;i<=m;i++)//对有意思的进行初始化
        {
            scanf("%d",&tmp);
            for(int j=0;j<tmp;j++){
               scanf("%d",&y);
               map[i][y]=1;
            }
        }
        for(int i=1;i<=m;i++)
        {
            memset(visited,0,sizeof(visited));//开始标记为全部没有访问
            if(find(i))    //查找当前的i是否可以匹配成功
                ans++;
        }
        printf("%s\n",ans>=m ? "YES" : "NO");
    }
    return 0;
}
时间: 2024-11-11 18:30:29

Courses(二分图水题)的相关文章

二分图水题三发

POJ1274 The Perfect Stall 题目大意:n个奶牛m个仓库,每个奶牛都有喜欢的仓库,当然一个奶牛只能住进一个仓库 ,一个仓库也只能让一个奶牛吃,问最多有多少奶牛能住进喜欢的仓库 思路:裸的匹配 练下模板 //poj1274 #include <stdio.h> #include <iostream> #include<queue> #include <string.h> #include <algorithm> #define

Poj_2239 Selecting Courses -二分图裸题

/************************************************ Author :DarkTong Created Time :2016/7/31 15:38:05 File Name :Poj_2339.cpp *************************************************/ //#include <bits/stdc++.h> #include <cstdio> #include <cstring>

POJ 2239--Selecting Courses【二分图 &amp;&amp; 最大匹配数 &amp;&amp; 水题】

Selecting Courses Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9363   Accepted: 4164 Description It is well known that it is not easy to select courses in the college, for there is usually conflict among the time of the courses. Li Mi

hdu Courses 1083 二分匹配 ,匈牙利算法。。水题

Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4233    Accepted Submission(s): 2014 Problem Description Consider a group of N students and P courses. Each student visits zero, one or

POJ 1274--The Perfect Stall【二分图 &amp;&amp; 最大匹配数 &amp;&amp; 水题】

The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20795   Accepted: 9386 Description Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering pr

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

poj2239 Selecting Courses --- 二分图最大匹配

匈牙利算法模板题 有n门课程,每门课程可能有不同一时候间,不同一时候间的课程等价. 问不冲突的情况下最多能选多少门课. 建立二分图,一边顶点表示不同课程,还有一边表示课程的时间(hash一下). #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include

2015南阳CCPC L - Huatuo&#39;s Medicine 水题

L - Huatuo's Medicine Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Huatuo was a famous doctor. He use identical bottles to carry the medicine. There are different types of medicine. Huatuo put medicines into the bottles and chain these b

sdut 2841 Bit Problem (水题)

题目 贴这个题是因为看题解有更简单的方法, 我做的时候是直接算的, 也很简单. 贴一下题解吧: 如果一个整数不等于 0,那么该整数的二进制表示中至少有一位是 1. 这个题结果可以直接输出 x - (x&(x-1)); 因为x-1 之后二进制下,就是最右边的1变成了0, 最右边的1的 右边所有的0变成了1, 不影响最左边. 我的代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4