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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 在eclipse中没有server(需在选项中设置)

    Eclipse中没有Server选项,需要加载插件。步骤如下:①在软件eclipse下的Help-&gt;InstallNewSoftware-&gt;中,在Workwith中点击Add,如下,加入Name:KeplerLocation:http://download.eclipse.org/releases/kepler②找到选项Web,XML,JavaEEan…

    2022年4月10日
    210
  • centos 7关闭selinux

    centos7关闭selinux查看SELinux状态:getenforce临时设置SeLinux:setenforce0永久修改Selinux为disabled:[root@localhost~]#cat/etc/selinux/config#ThisfilecontrolsthestateofSELinuxonthesystem.#SEL…

    2022年4月7日
    44
  • C# winform 界面美化技巧(扁平化设计)

    C# winform 界面美化技巧(扁平化设计)C#winform界面美化技巧(扁平化设计)关于C#界面美化的一些小技巧在不使用第三方控件如IrisSkin的前提下,依然可以对winform做出让人眼前一亮的美化首先,我们先来实现主界面的扁平化此处分为两个步骤,第一步是更改winform自带的MainForm窗体属性,第二步是添加窗体事件。将主窗体FormBorderStyle更改为None,这样就得到了一个无边框的窗体(w…

    2022年5月28日
    39
  • 第二范式和bcnf范式区别(第二范式改第三范式)

    文章系转载,通俗易懂,写了很久代码,数据库知识一直薄弱,学习中;本文能够很好帮助理解,谢谢原作者!转载WencoChen发布于2019-03-0216:08:45阅读数4353收藏展开文章目录什么是”范式(NF)” 1.第一范式(1NF) 2.第二范式(2NF) 2.1函数依赖 2.1.1完全函数依赖 2.1.2部分函数依赖 2….

    2022年4月16日
    44
  • 国外那些优秀的 Drupal 教程博客

    国外那些优秀的 Drupal 教程博客使用开源软件的巨大好处之一,就是能够参与到它们强大的交流社区中。作为开源CMS的领军人物,Drupal社区就是很好的例子。随着 Drupal 不断地发展变化,社区里的成员每天也都在进行着各种交流,例如Drupal 的新特性、如何使用现有的功能、怎样能够让这个平台变得更好等等。“分享”是开源的核心精神,因此很多人也会将自己的经验、心得以及各种相关的内容记录到博客中。  如果你想加强对 

    2022年6月12日
    34
  • Linux常用命令详解_shell基本命令的使用

    Linux常用命令详解_shell基本命令的使用一、日常使用命令/常用快捷键命令开关机命令    1、shutdown–hnow:立刻进行关机     2、shutdown–rnow:现在重新启动计算机     3、reboot:现在重新启动计算机     4、su-:切换用户;passwd:修改用户密码     5、logout:用户注销常用快捷命令     1、…

    2022年9月30日
    5

发表回复

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

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