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 ...
随机推荐
- Vue directive自定义指令+canvas实现H5图片压缩上传-Base64格式
前言 最近优化项目-手机拍照图片太大,回显速度比较慢,使用了vue的自定义指令实现H5压缩上传base64格式的图片 canvas自定义指令 Vue.directive("canvas&qu ...
- scala 获取当前时间的两种方式
在编写程序时,有时需要获取当前时间,这在记录异常信息.获取程序运行耗时很有用处 方式一: val time1=System.currentTimeMillis() 这种方式获取的是程序运行到此的毫秒数 ...
- PADS随记
在PADS,PCB板设计中,怎么一次就把丝印的大小设置好? CTRL+ALT+F 组合键 打开 选择过滤器(Filter) 如下图 去掉其他的勾选,只选择 Labels . 之后在板子上鼠标拖动选上 ...
- Python学习手册之捕获组和特殊匹配字符串
在上一篇文章中,我们介绍了 Python 的字符类和对元字符进行了深入讲解,现在我们介绍 Python 的捕获组和特殊匹配字符串.查看上一篇文章请点击:https://www.cnblogs.com/ ...
- Python2和Python3
1. 字符编码 1.1. Python2默认为ACSII编码 1.2. Python3为Unicode 2. Unicode和UTF8和GBK编码的关系 utf8:中文3字节 ...
- Python递归二分法
# lst = [4, 56, 178, 253, 625, 1475, 2580, 3574, 15963] # 时间复杂度. n# # 让用户输入一个数n. 判断这个n是否出现在lst中# n = ...
- 数据结构之 AVL个人笔记
从这位前辈的博客园中学习的数据结构:https://www.cnblogs.com/skywang12345/ 非常感谢这位前辈. 以下文章摘录于 :skywang12345的博客园:转载请注明出处: ...
- go内建容器-切片
1.基础定义 看到'切片'二字,满脸懵逼.切的啥?用的什么刀法切?得到的切片有什么特点?可以对切片进行什么操作? 先看怎么得到切片,也就是前两个问题.切片的底层是数组,所以切片切的是数组:切的时候采用 ...
- 成都Uber优步司机奖励政策(3月15日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Java语言简介
Java即计算机编程语言 1.概念 Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承.指针等概念,因此Java语言具有功能强大和简单易用两个特征.Jav ...