洛谷——P2417 课程
P2417 课程
裸地匈牙利算法,
贪心的不断匹配,若没匹配,则匹配;反之,判断与之匹配的点能否在找另一个点匹配,若能,抢多这个点与之匹配
时间复杂度$O(n\times m)$
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 2000000
using namespace std; struct node{
int to,next;
}e[N];
int t,head[N],tot,p,n,match[N],dfn[N],ans; void add(int u,int v){
e[++tot].to=v,e[tot].next=head[u],head[u]=tot;
} bool dfs(int u,int t){
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(dfn[v]!=t){
dfn[v]=t;
if(!match[v]||dfs(match[v],t)){
match[v]=u;return true;
}
}
}
return false;
} int main()
{
scanf("%d",&t);
while(t--){
memset(head,,sizeof(head));
memset(match,,sizeof(match));
memset(dfn,,sizeof(dfn));
tot=ans=;
scanf("%d%d",&p,&n);
for(int m,v,i=;i<=p;i++){
scanf("%d",&m);
for(int j=;j<=m;j++){
scanf("%d",&v),add(v,i);
}
}
for(int i=;i<=n;i++)
if(dfs(i,i)) ++ans;
if(ans>=p) printf("YES\n");
else printf("NO\n");
} return ;
}
P2071 座位安排
模板,又是模板,建图方式有一点儿不同,因为每一排有两个座位
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 2000000
using namespace std; struct node {
int to,next;
} e[N];
int t,head[N],tot,p,n,match[N],dfn[N],ans; void add(int u,int v) {
e[++tot].to=v,e[tot].next=head[u],head[u]=tot;
} bool dfs(int u,int t) {
for(int i=head[u]; i; i=e[i].next) {
int v=e[i].to;
if(dfn[v]!=t) {
dfn[v]=t;
if(!match[v]||dfs(match[v],t)) {
match[v]=u;
return true;
}
}
}
return false;
} int main() {
memset(head,,sizeof(head));
memset(match,,sizeof(match));
memset(dfn,,sizeof(dfn));
tot=ans=;
scanf("%d",&n);
for(int x,y,i=; i<=n*; i++) {
scanf("%d%d",&x,&y);
add(i,x),add(i,x+n),add(i,y),add(i,y+n);
}
for(int i=; i<=n*; i++)
if(dfs(i,i)) ++ans;
printf("%d\n",ans); return ;
}
P1894 [USACO4.2]完美的牛栏The Perfect Stall
裸题
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 2000000
using namespace std; struct node {
int to,next;
} e[N];
int t,head[N],tot,m,n,match[N],dfn[N],ans; void add(int u,int v) {
e[++tot].to=v,e[tot].next=head[u],head[u]=tot;
} bool dfs(int u,int t) {
for(int i=head[u]; i; i=e[i].next) {
int v=e[i].to;
if(dfn[v]!=t) {
dfn[v]=t;
if(!match[v]||dfs(match[v],t)) {
match[v]=u;
return true;
}
}
}
return false;
} int main() {
memset(head,,sizeof(head));
memset(match,,sizeof(match));
memset(dfn,,sizeof(dfn));
tot=ans=;
scanf("%d%d",&n,&m);
for(int v,x,i=; i<=n; i++) {
scanf("%d",&x);
for(int j=;j<=x;j++) {
scanf("%d",&v);
add(i,v);
}
}
for(int i=; i<=n; i++)
if(dfs(i,i)) ++ans;
printf("%d\n",ans); return ;
}
洛谷——P2417 课程的更多相关文章
- 洛谷—— P2417 课程
https://www.luogu.org/problemnew/show/2417 题目描述 n个学生去p个课堂,每一个学生都有自己的课堂,并且每个学生只能去一个课堂,题目要求能够安排每一个课堂都有 ...
- [洛谷P2417]课程
题目链接: 点我 题目分析: 二分图最大匹配裸题,跑完匈牙利判断\(ans\)是否等于教室数即可 多组数据请注意初始化. 代码: #include<bits/stdc++.h> #defi ...
- USACO Section 1.3 题解 (洛谷OJ P1209 P1444 P3650 P2693)
usaco ch1.4 sort(d , d + c, [](int a, int b) -> bool { return a > b; }); 生成与过滤 generator&& ...
- 洛谷 P2014 选课(树形背包)
洛谷 P2014 选课(树形背包) 思路 题面:洛谷 P2014 如题这种有依赖性的任务可以用一棵树表示,因为一个儿子要访问到就必须先访问到父亲.然后,本来本题所有树是森林(没有共同祖先),但是题中的 ...
- 树形DP 洛谷P2014 选课
洛谷P2014 选课 题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门 ...
- C++ 洛谷 2014 选课 from_树形DP
洛谷 2014 选课 没学树形DP的,看一下. 首先要学会多叉树转二叉树. 树有很多种,二叉树是一种人人喜欢的数据结构,简单而且规则.但一般来说,树形动规的题目很少出现二叉树,因此将多叉树转成二叉树就 ...
- 洛谷1640 bzoj1854游戏 匈牙利就是又短又快
bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
- 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.
没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...
- 洛谷P1108 低价购买[DP | LIS方案数]
题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...
随机推荐
- JS处理时间相关
<script>var d=new Date(); alert(d);alert(d.getMonth());alert(d.getHours());alert(d.getYear()); ...
- bzoj 3677: [Apio2014]连珠线【树形dp】
参考:http://www.cnblogs.com/mmlz/p/4456547.html 枚举根,然后做树形dp,设f[i][1]为i是蓝线中点(蓝线一定是父子孙三代),f[i][0]为不是,转移很 ...
- Activiti6.0教程 28张表解析 (三)
使用Activit的朋友都知道Activiti对应的有28张表,今天我们就来说下Activit中28张表对应的含义是什么? 如何创建表? 在Activiti中创建表有三种方式,我们依次来看下: 一.通 ...
- Python测试工具——nose
1.nose 特点: a) 自动发现测试用例(包含[Tt]est文件以及文件包中包含test的函数) b) 以test开头的文件 c) 以test开头的 ...
- 51nod 1096 距离之和最小
求中位数,注意求中位数前排序.... #include <bits/stdc++.h> using namespace std; #define LL long long const in ...
- JAVA大数处理(BigInteger,BigDecimal)
原文链接 Java中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类. 这两个类都在java.math.*包中,因此每次必须在开头处引用该包. Ⅰ基本函数: 1.valu ...
- 416 Partition Equal Subset Sum 分割相同子集和
详见:https://leetcode.com/problems/partition-equal-subset-sum/description/ C++: class Solution { publi ...
- [转]Asp.net MVC中Html.Partial, RenderPartial, Action,RenderAction 区别和用法
本文转自:http://www.cnblogs.com/gesenkof99/archive/2013/06/03/3115052.html Partial 和RenderPartial:这两个的性质 ...
- 调用wsdl接口,参数是xml格式
1.最近太累了,好困.闲话少许直奔主题吧.上代码 try{ String wsurl = "http://172.16.16.236:9999/xxx/ws/WSService?wsdl&q ...
- poj2886 Who Gets the Most Candies?
思路: 先打反素数表,即可确定因子最多的那个数.然后模拟踢人的过程确定对应的人名.模拟的过程使用线段树优化加速. 实现: #include <cstdio> #include <cs ...