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

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. Vue learning experience

    一.内置指令[v-ref] Official-document-expression: 父组件在子组件上注册的索引,便于直接访问.不需要表达式,必须提供参数ID,可以通过父组件的$ref对象访问子组件 ...

  2. 能够还原jQuery1.8的toggle的功能的插件

    下面这个jQuery插件能够还原1.8的toggle的功能,如果你需要,可以直接把下面这段代码拷贝到你的jQuery里面,然后跟平时一样使用toggle的功能即可. //toggle plugin f ...

  3. python 新手函数基础(函数定义调用值传递等)

    1.编程的集中主要方式: 面向过程 >类 >>关键字class 面向函数>函数 >> 关键字def 面向过程> 过程 >> 关键字def 2.py ...

  4. transient是干嘛的

    Java的serialization提供了一种持久化对象实例的机制.当持久化对象时,可能有一个特殊的对象数据成员,我们不想用 serialization机制来保存它.为了在一个特定对象的一个域上关闭s ...

  5. 20145226夏艺华 JAVA预备作业1

    博客阅读总结 关于师生关系: 学生和老师之间我觉得关系时多元化的,不能拘泥于单独的一种关系:灌输与被灌输,教授与被教授--我认为,在不同的课程阶段,师生之间的关系都可以发生变化.前期的老师更像是一个指 ...

  6. 2 引用 深copy 浅copy

    1. is == 总结 is 是比较两个引用是否指向了同一个对象(引用比较). == 是比较两个对象是否相等. In [1]: a = [11,22,33] In [2]: b = [11,22,33 ...

  7. CodeForces 547D Mike and Fish 思维

    题意: 二维平面上给出\(n\)个点,然后对每个点进行染色:红色和蓝色,要求位于同一行或同一列的点中,红色点和蓝色点的个数相差不超过1 分析: 正解是求欧拉路径,在这篇博客中看到一个巧妙的思路: 对于 ...

  8. hive整合sentry,impala,hue之后权限管理操作

    7.Hive授权参考(开启sentry之后,对用户授权用不了,只能针对用户组,grant role testrole to user xxxxxxx; ) 7.1:角色创建和删除 create rol ...

  9. 5、Java并发编程:Lock

    Java并发编程:Lock 在上一篇文章中我们讲到了如何使用关键字synchronized来实现同步访问.本文我们继续来探讨这个问题,从Java 5之后,在java.util.concurrent.l ...

  10. Java - 无乱码读写文件

    Java读取数据流的时候,一定要指定数据流的编码方式,否则将使用本地环境中的默认字符集. BufferedReader reader = null; String laststr = "&q ...