匈牙利算法(二分图匹配)

C - Courses

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:

Input

2
3 3
3 1 2 3
2 1 2
1 1
3 3
2 1 3
2 1 3
1 1

Output

YES
NO

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
 #include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f3f int G[][],vis[],used[];
int n,p;
bool find(int u)//核心部分:寻找匈牙利算法的增广路
{
int i;
for(i=;i<=n;i++)
{
if(!vis[i] && G[u][i])
{
vis[i]=;
if(!used[i] || find(used[i]))
{
used[i]=u;
return true;
}
}
}
return false;
}
int main()
{
int a,b,i,ans,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&p,&n);
memset(G,,sizeof(G));
ans=;
for(i=;i<=p;i++)
{
scanf("%d",&a);
while(a--)
{
scanf("%d",&b);
G[i][b]=;
}
}
memset(used,,sizeof(used));
for(i=;i<=p;i++)
{
memset(vis,,sizeof(vis));
if(find(i)) ans++;
}
if(ans==p) printf("YES\n");
else printf("NO\n");
}
return ;
}

B - The Accomodation of Students

There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.

Now you are given all pairs of students who know each other. Your
task is to divide the students into two groups so that any two students
in the same group don't know each other.If this goal can be achieved,
then arrange them into double rooms. Remember, only paris appearing in
the previous given set can live in the same room, which means only known
students can live in the same room.

Calculate the maximum number of pairs that can be arranged into these double rooms.

InputFor each data set:

The first line gives two integers, n and m(1<n<=200),
indicating there are n students and m pairs of students who know each
other. The next m lines give such pairs.

Proceed to the end of file.

OutputIf these students cannot be divided into two groups, print
"No". Otherwise, print the maximum number of pairs that can be arranged
in those rooms.

Sample Input

4 4
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6

Sample Output

No
3
 #include<stdio.h>
#include<string.h>
#include <queue>
using namespace std;
bool vis[];
int G[][],used[];
int n,m;
int find(int u)//核心部分:寻找匈牙利算法的增广路
{
int i,n;
for(i=; i<=n; i++)
{
if(!vis[i] && G[u][i])
{
vis[i]=true;
if(!used[i] || find(used[i]))
{
used[i]=u;
return ;
}
}
}
return ;
} int judge[];
bool bfs()//广度优先搜索进行
{
memset(judge,-,sizeof(judge));
queue<int> q;
q.push();
judge[]=;
while(!q.empty())
{
int v=q.front();
q.pop();
for(int i=; i<=n; i++)
{
if(G[v][i])
{
if(judge[i]==-)
{
judge[i]=(judge[v]+)%;
q.push(i);
}
else
{
if(judge[i]==judge[v])
return false;
} }
}
}
return true;
} int main()
{ while(~scanf("%d%d",&n,&m))
{
memset(G,,sizeof(G));
memset(used,,sizeof(used));
int a,b;
for(int i=; i<m; i++)
{
scanf("%d%d",&a,&b);
G[a][b]=;
G[b][a]=;
}
if(!bfs())
{
puts("No");
continue;
}
int ans=;
for(int i=; i<=n; i++)
{
memset(vis,,sizeof(vis));
if(find(i));
ans++;
}
printf("%d\n",ans/);
}
return ;
}

ACM 第五天的更多相关文章

  1. ACM第五次积分赛

    做出三道题,第二名,总积分上升到第八名,继续加油! SAU-ACM总比赛成绩 姓名     账号  上学期成绩 第一次成绩 第二次成绩 第三次成绩 第四次成绩 第五次成绩 总成绩 张国庆 143401 ...

  2. 大一暑假为期五周的ACM实验室培训结束了(2013.8.24)

    没想到,我的大学里第一个暑假,9周的时间只有最初的两周在家待着,接下来的7周将会在学校度过. 说真的,这是我上学以来,第一次真正好好利用的假期.在这五周里,周一.三.五下午学长都会给我们讲点知识,之后 ...

  3. 牛客网暑期ACM多校训练营(第五场):F - take

    链接:牛客网暑期ACM多校训练营(第五场):F - take 题意: Kanade有n个盒子,第i个盒子有p [i]概率有一个d [i]大小的钻石. 起初,Kanade有一颗0号钻石.她将从第1到第n ...

  4. 2018.11.16 浪在ACM 集训队第五次测试赛

    2018.11.16 浪在ACM 集训队第五次测试赛 整理人:李继朋 Problem A : 参考博客:[1]朱远迪 Problem B : 参考博客: Problem C : 参考博客:[1]马鸿儒 ...

  5. UESTC-第五届ACM趣味程序设计竞赛第四场(正式赛)--不完全解题报告

    比赛链接: http://acm.uestc.edu.cn/contest.php?cid=230 A.Police And The Thief ---UESTC 1913 简单博弈,先假设在警察先走 ...

  6. 2018牛客网暑假ACM多校训练赛(第五场)H subseq 树状数组

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round5-H.html 题目传送门 - https://www.no ...

  7. 2018牛客网暑假ACM多校训练赛(第五场)F take 树状数组,期望

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round5-F.html 题目传送门 - https://www.no ...

  8. 2018牛客网暑期ACM多校训练营(第五场) F - take - [数学期望][树状数组]

    题目链接:https://www.nowcoder.com/acm/contest/143/F 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

  9. 2018牛客网暑期ACM多校训练营(第五场) E - room - [最小费用最大流模板题]

    题目链接:https://www.nowcoder.com/acm/contest/143/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

随机推荐

  1. js 校验身份证号

    根据地区编码.身份证格式.18位身份证需要验证最后一位校验位 //校验身份证 function IdentityCodeValid(code) { var city = { 11: "北京& ...

  2. Elasticsearch 6 重要参数配置

    采用zip或tar.gz的二进制包方式安装的ES,需要配置一系列参数,其中重要参数配置如下: 一. ElasticSearch参数配置 1. data和logs路径配置 如果使用.zip或.tar.g ...

  3. 【Spark】源码分析之RDD的生成及stage的切分

    一.概述 Spark源码整体的逻辑(spark1.3.1): 从saveAsTextFile()方法入手 -->saveAsTextFile()  --> saveAsHadoopFile ...

  4. Go 入门 - Go中的复杂类型

    主要内容来自中文版的官方教程Go语言之旅 目的为总结要点 指针 Go 拥有指针.指针保存了值的内存地址. 类型 *T 是指向 T 类型值的指针.其零值为 nil. var p *int & 操 ...

  5. tomcat8.5配置高并发

    最近部署的tomcat应用,有一天压测的时候,测试一致反馈下载不了,结果查看日志才发现如下错误: INFO: Maximum number of threads (200) created for c ...

  6. 韩国KT软件NB-IOT开发记录V150(2)IOT maker通信相关

    1. 测试的AT指令,创建端口和IP地址链接 AT#IMINIT=," 开始连接 AT#IMCONN 创建object ID AT#IMOBJMETA=,," 发送数据 AT#IM ...

  7. python简单的socket 服务器和客户端

    服务器端代码 if "__main__" == __name__: try: sock = socket.socket(socket.AF_INET, socket.SOCK_ST ...

  8. Consul初体验

      Preface       Today I'm gonna implement a consul in my environment to discover service of MySQL da ...

  9. python3 爬虫爬取深圳公租房轮候库(深圳房网)

    深圳公租房轮候库已经朝着几十万人的规模前进了,这是截至16年10月之前的数据了,贴上来大家体会下 所以17年已更新妥妥的10W+ 今天就拿这个作为爬虫的练手项目 1.环境准备: 操作系统:win10 ...

  10. Appium(Python)驱动手机Chrome浏览器

    手机Chrome浏览器访问淘宝H5与在电脑上访问淘宝H5是一摸一样的: 第一种方法: 直接在电脑Chrome浏览器上打开F12: 第二种方法: 手机连接电脑后, 在手机Chrome浏览器上打开淘宝H5 ...