ACM 第五天
匈牙利算法(二分图匹配)
C - Courses
. 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:
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
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.
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 第五天的更多相关文章
- ACM第五次积分赛
做出三道题,第二名,总积分上升到第八名,继续加油! SAU-ACM总比赛成绩 姓名 账号 上学期成绩 第一次成绩 第二次成绩 第三次成绩 第四次成绩 第五次成绩 总成绩 张国庆 143401 ...
- 大一暑假为期五周的ACM实验室培训结束了(2013.8.24)
没想到,我的大学里第一个暑假,9周的时间只有最初的两周在家待着,接下来的7周将会在学校度过. 说真的,这是我上学以来,第一次真正好好利用的假期.在这五周里,周一.三.五下午学长都会给我们讲点知识,之后 ...
- 牛客网暑期ACM多校训练营(第五场):F - take
链接:牛客网暑期ACM多校训练营(第五场):F - take 题意: Kanade有n个盒子,第i个盒子有p [i]概率有一个d [i]大小的钻石. 起初,Kanade有一颗0号钻石.她将从第1到第n ...
- 2018.11.16 浪在ACM 集训队第五次测试赛
2018.11.16 浪在ACM 集训队第五次测试赛 整理人:李继朋 Problem A : 参考博客:[1]朱远迪 Problem B : 参考博客: Problem C : 参考博客:[1]马鸿儒 ...
- UESTC-第五届ACM趣味程序设计竞赛第四场(正式赛)--不完全解题报告
比赛链接: http://acm.uestc.edu.cn/contest.php?cid=230 A.Police And The Thief ---UESTC 1913 简单博弈,先假设在警察先走 ...
- 2018牛客网暑假ACM多校训练赛(第五场)H subseq 树状数组
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round5-H.html 题目传送门 - https://www.no ...
- 2018牛客网暑假ACM多校训练赛(第五场)F take 树状数组,期望
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round5-F.html 题目传送门 - https://www.no ...
- 2018牛客网暑期ACM多校训练营(第五场) F - take - [数学期望][树状数组]
题目链接:https://www.nowcoder.com/acm/contest/143/F 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...
- 2018牛客网暑期ACM多校训练营(第五场) E - room - [最小费用最大流模板题]
题目链接:https://www.nowcoder.com/acm/contest/143/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...
随机推荐
- 02-HTTP的请求方法以及响应状态码
1. HTTP的请求方法以及响应状态码 1.1. 请求方法 http请求方法有GET.POST.PUT.HEAD.DELETE.OPTIONS.TRACE.CONNECT.当然上述方法是基于HTT ...
- css 浮动说明
clear:both; 1.要了解的:什么是浮动.浮在某面板之上. 例如:float:left; 向左停靠, 就是让需要设置浮动的元素,跟在指定元素后面. 先上实例: 比较常用导航: .nav_ul ...
- Facade(外观模式或门面模式)
常用的模式之一. 为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 完美地体现了依赖倒转原则和迪米特法则的思想. Facade模式应用场景: 首先 ...
- day06-codes and exercise in class
# Author: Ghost # Email: jiaci.liu@gmail.com ''' 1-Review of last week 2-interface class, abstract c ...
- Python基础、条件语句和基本数据类型
1. 第一句python - 后缀名是可以是任意? - 导入模块时,如果不是.py文件 ==> 以后文件后缀名是 .py 2. 两种执行方式 python解释器 py文件路径 python 进入 ...
- 分享Centos6.5升级glibc过程
默认的Centos6.5 glibc版本最高为2.12, 而在进行Nodejs开发时项目所依赖的包往往需要更高版本的glibc库支持, 因此在不升级系统的前提下, 需要主动更新系统glibc库. 一般 ...
- Java-谈谈对Java平台的理解
问题 谈谈对 Java 平台的理解 Java是解释执行的 这句话对么 程序的编译与解释有什么区别 Java 平台的了解 Java的主要特点是两个, 编写一次到处运行 Write once, run a ...
- CSS-cascading stle sheets
CSS-cascading stle sheets 1. CSS 什么是CSS?CSS 指层叠样式表 (Cascading Style Sheets) 样式定义如何显示 HTML 元素 样式 ...
- FPGA学习之路——PLL的使用
锁相环(PLL)主要用于频率综合,使用一个 PLL 可以从一个输入时钟信号生成多个时钟信号. PLL 内部的功能框图如下图所示: 在ISE中新建一个PLL的IP核,设置四个输出时钟,分别为25MHz. ...
- mac, start sublime from terminal
1.where is sublime CLI /Applications/Sublime Text.app/Contents/SharedSupport/bin/subl 2. run sublime ...