POJ1469_COURSES(二部图最大匹配)

POJ1469_COURSES(二部图最大匹配)

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

解决报告

http://blog.csdn.net/juncoder/article/details/38136065

题目传送门

题意:

n个学生p门课程,每一个学生学习0或1以上的课程。

问:能否够组成委员会。满足

每一个学生代表一门不同的课程

一门课程在委员会中有一名代表

思路:

非常明显的二分图的完备匹配。

#include <map>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 330
#define P 110
using namespace std;
int mmap[N+P][N+P],n,p,pre[N+P],vis[N+P],m,k;
int dfs(int x)
{
    for(int i=p+1;i<=p+n;i++)
    {
        if(!vis[i]&&mmap[x][i])
        {
            vis[i]=1;
            if(pre[i]==-1||dfs(pre[i]))
            {
                pre[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int i,j,t;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            memset(mmap,0,sizeof(mmap));
            memset(pre,-1,sizeof(pre));
            scanf("%d%d",&p,&n);
            for(i=1;i<=p;i++)
            {
                scanf("%d",&m);
                for(j=1;j<=m;j++)
                {
                    scanf("%d",&k);
                    mmap[i][k+p]=1;
                }
            }
            int ans=0;
            for(i=1;i<=p;i++)
            {
                memset(vis,0,sizeof(vis));
                ans+=dfs(i);
            }
            if(ans==p)
            printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}

COURSES
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 17166   Accepted: 6748

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 

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: 

P N 

Count1 Student
1 1 Student
1 2 … Student
1 Count1 

Count2 Student
2 1 Student
2 2 … Student
2 Count2 

… 

CountP Student
P 1 Student
P 2 … Student
P 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抣l 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. 

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116823.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 独立成分分析 ( ICA ) 与主成分分析 ( PCA ) 的区别「建议收藏」

    独立成分分析 ( ICA ) 与主成分分析 ( PCA ) 的区别「建议收藏」1.前言参考资料:https://www.zhihu.com/question/28845451书上写的是:1.主成分分析假设源信号间彼此非相关,独立成分分析假设源信号间彼此独立。2.主成分分析认为主元之间彼此正交,样本呈高斯分布;独立成分分析则不要求样本呈高斯分布。在利用最大化信息熵的方法进行独立成分分析的时候,需要为源信号假定一个概率密度分布函数g’,进而找出使得g(Y)=g…

    2022年5月13日
    48
  • mybatis 查询返回List集合、map集合、List<Map>集合[通俗易懂]

    mybatis 查询返回List集合、map集合、List<Map>集合[通俗易懂]主要是加上这一行:resultType=“java.lang.String” <!–查询所有的学生去向–> <selectid=”selectStudentDirection”resultType=”java.lang.String”> SELECTdirectionNamefromwork_direction </select>…

    2022年9月27日
    5
  • Java最新手机号正则验证[通俗易懂]

    Java最新手机号正则验证[通俗易懂] 中国电信号段133、149、153、173、177、180、181、189、199中国联通号段130、131、132、145、155、156、166、175、176、185、186中国移动号段134(0-8)、135、136、137、138、139、147、150、151、152、157、158、159、178、182、183、184、187、188、198其他号段14号…

    2022年6月12日
    31
  • mysql长轮询_ajax的轮询和长轮询

    mysql长轮询_ajax的轮询和长轮询概念:轮询(polling):客户端按规定时间定时像服务端发送ajax请求,服务器接到请求后马上返回响应信息并关闭连接。概念总是枯燥的,只有代码方能解心头之快前段代码:index.html:vargetting={url:’server.php’,dataType:’json’,success:function(res){console.log(res);}};//关键在这里,Ajax定时…

    2022年10月9日
    2
  • iis命令停止启动_更新并重启怎么取消

    iis命令停止启动_更新并重启怎么取消直接使用CMD我们可以操作很多事情,比如启动IIS,重启IIS,停止IIS重启IIS服务器,开始->运行->cmd(以下列出相关操作命令):iisreset /RESTART停止后启动iisreset/START启动IIS(如果停止)iisreset/STOP停止IIS(如果启动)iisreset/REBOOT重启电脑iisreset/REB…

    2025年5月23日
    2
  • xshell 在Oracle SQL Plus backspace键 变为 sele^H^H^H

    xshell 在Oracle SQL Plus backspace键 变为 sele^H^H^Hxshell在OracleSQLPlus backspace键变为sele^H^H^H问题描述:用Xshell登录进入linux后,在普通模式下或进入SQLPlus 模式下,对输入进行删除等操作没有问题.而在运行中,按delete,backspace键时会产生^H等乱码问题.这是因为编码不匹配的问题.解决方法:方法1:

    2025年5月28日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号