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

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. 创建jq插件步骤

    无意看了这篇<jQuery插件开发精品教程,让你的jQuery提升一个台阶>文章,现在做一下总结. 一.jQuery插件的创建可以有三种方法 1.通过$.extend()来扩展jQuery ...

  2. spring-mvc高级技术

    Spring MVC高级技术包括但不限于web.xml配置.异常处理.跨重定向请求传递数据 1.web.xml文件的配置 <!DOCTYPE web-app PUBLIC "-//Su ...

  3. C语言Windows程序开发—MessageBox函数介绍【第01天】

    (一)MessageBox函数的参数介绍: int MessageBox ( HWND hWnd, //弹出MessageBox对话框所属的窗口句柄 LPCTSTR lpText, //指向Messa ...

  4. [Real World Haskell翻译]第24章 并发和多核编程 第一部分并发编程

    第24章 并发和多核编程 第一部分并发编程 当我们写这本书的时候,CPU架构正在以比过去几十年间更快的速度变化. 并发和并行的定义 并发程序需要同时执行多个不相关任务.考虑游戏服务器的例子:它通常是由 ...

  5. 上海Uber优步司机奖励政策(1月4日~1月10日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. AtCoder:C - Nuske vs Phantom Thnook

    C - Nuske vs Phantom Thnook https://agc015.contest.atcoder.jp/tasks/agc015_c 题意: n*m的网格,每个格子可能是蓝色, 可 ...

  7. python学习五

    打包代码与数据 数据结构要与数据匹配,数据结构影响代码的复杂性   列表 集合 字典 #创建与初始化 cleese={} cleese2=dict() cleese["name"] ...

  8. Ruby基础教程 1-10

    类结构 1.数值类结构     Fixnum到Bignum会自动转换   2.常用数值表示   3. ans=10.divmod(3) ans[0]是商  ans[1]是余数   4.实例方法roun ...

  9. 使用union 外加count

    explain extended and name='aaa')) t; +----+--------------+------------+-------+---------------+----- ...

  10. selenium,unittest——自动化执行多个py文件脚本并生成报告

    将多个py文件的自动化脚本顺序运行,并生成报告,运行run_all_case后会自动运行文件内所有test开头的py文件并在指定文件夹report生成由脚本时间命名的报告 脚本执行后结果: 生成报告并 ...