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


相关推荐

  • 【蓝牙sbc协议】sbc源码阅读笔记(二)——sbc_struct详解(上)[通俗易懂]

    【蓝牙sbc协议】sbc源码阅读笔记(二)——sbc_struct详解(上)[通俗易懂]sbc_struct结构详解sbc_struct结构的定义://sbc.hstructsbc_struct{ unsignedlongflags; uint8_tfrequency; uint8_tblocks; uint8_tsubbands; uint8_tmode; uint8_tallocation; uint8_tbitpool; uint8_tendian; void*priv; void*priv_alloc_base;};typ

    2022年9月11日
    0
  • pycharm激活码2021-激活码分享

    (pycharm激活码2021)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月20日
    184
  • 按钮的几种状态的区别

    按钮的几种状态的区别

    2021年9月16日
    61
  • 扫清盲点,如何正确的从HttpClient 3.x系统升级到HttpClient 4.x

    扫清盲点,如何正确的从HttpClient 3.x系统升级到HttpClient 4.x如果周期比较长的项目,或者这个项目开发人员换过了好几拨人,很有可能出现一些奇怪的问题,比如一个项目中出现了多种Spring注入bean的方式,不同版本的jar冲突等等爬虫项目有的时候更是过犹不及,拿模拟登陆来说,开发人员的迭代,每个人的风格和技术各不相同,模拟登陆的方式也是五花八门,早在之前看到过一个项目的源码,其中使用HttpClient也是各种风格,虽然官方已经强烈建议使用HttpClie…

    2022年7月22日
    11
  • VirtualBox虚拟机上网设置

    VirtualBox虚拟机上网设置VirtualBox虚拟机中如何上网:    安装了两个虚拟机后,如何让它们都能通过主机上网呢?有以下两种方法:a) NAT方式:该方式是利用宿主机的一个端口进行网络转发,虚拟机和主机共享一个ip地址,主机和虚拟机是不可见的,在互联网上他们是一台主机,在局域网内他们是互不相同的。那么在虚拟机中的设置是:点击虚拟机中的”设置”->”网络”->“连接方式”->”NAT”。然后进入虚拟机

    2022年5月12日
    36
  • Python实现 —【简易】12306爬虫[通俗易懂]

    Python实现 —【简易】12306爬虫[通俗易懂]最近这几天,学习了一下python,对于爬虫比较感兴趣,就做了一个简单的爬虫项目,因为快过年了么,要买回家的火车票,所以呢,通过分析12306网站,写了一个爬虫,现在,就将代码贴出来,分析的过程就不详细的介绍了,就是通过chorme浏览器进行分析。1#-*-coding:utf-8-*-2#@Date:2016-12-2714:26:333…

    2022年5月5日
    121

发表回复

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

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