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 课程的更多相关文章

  1. 洛谷—— P2417 课程

    https://www.luogu.org/problemnew/show/2417 题目描述 n个学生去p个课堂,每一个学生都有自己的课堂,并且每个学生只能去一个课堂,题目要求能够安排每一个课堂都有 ...

  2. [洛谷P2417]课程

    题目链接: 点我 题目分析: 二分图最大匹配裸题,跑完匈牙利判断\(ans\)是否等于教室数即可 多组数据请注意初始化. 代码: #include<bits/stdc++.h> #defi ...

  3. 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&& ...

  4. 洛谷 P2014 选课(树形背包)

    洛谷 P2014 选课(树形背包) 思路 题面:洛谷 P2014 如题这种有依赖性的任务可以用一棵树表示,因为一个儿子要访问到就必须先访问到父亲.然后,本来本题所有树是森林(没有共同祖先),但是题中的 ...

  5. 树形DP 洛谷P2014 选课

    洛谷P2014 选课 题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门 ...

  6. C++ 洛谷 2014 选课 from_树形DP

    洛谷 2014 选课 没学树形DP的,看一下. 首先要学会多叉树转二叉树. 树有很多种,二叉树是一种人人喜欢的数据结构,简单而且规则.但一般来说,树形动规的题目很少出现二叉树,因此将多叉树转成二叉树就 ...

  7. 洛谷1640 bzoj1854游戏 匈牙利就是又短又快

    bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...

  8. 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.

    没有上司的舞会  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...

  9. 洛谷P1108 低价购买[DP | LIS方案数]

    题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...

随机推荐

  1. mysql error:You can't specify target table for update in FROM clause

    mysql中You can't specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表( ...

  2. bzoj 1923: [Sdoi2010]外星千足虫【高斯消元】

    裸的异或高斯消元 #include<iostream> #include<cstdio> using namespace std; const int N=2005; int ...

  3. bzoj2002 [Hnoi2010]Bounce 弹飞绵羊【分块】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 这一题除了LCT解法,还有一种更巧妙,代码量更少的解法,就是分块.先想,如果仅仅记录每 ...

  4. tac命令的实现 分类: linux 2014-06-02 00:08 344人阅读 评论(0) 收藏

    此程序实现简化的linux中的tac命令.即对文件按行倒序输出. 首先将文件指针置于文件尾,从后向前移动指针, 将两个换行符'\n'间的内容作为一行输出. #include<stdio.h> ...

  5. java中的位预算

    public class Demo { public static void main(String[] args) { byte num1 = 3; byte num2 = 5; /*位预算 *nu ...

  6. Linux普通到root用户切换-转

    1.Linux中的用户切换:su和su - 的区别 大部分Linux发行版的默认账户是普通用户,而更改系统文件或者执行某些命令,需要root身份才能进行,这就需要从当前用户切换到root用户,Linu ...

  7. hdu 5036 Explosion bitset优化floyd

    http://acm.hdu.edu.cn/showproblem.php?pid=5036 题意就是给定一副有向图,现在需要走遍这n个顶点,一开始出发的顶点是这n个之中的随便一个. 如果走了1,那么 ...

  8. C#_JDBC连接数据库

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. 工作记录:JS正则表达式 angularjs ng-if ng-show ng-switch

    用了一下JS 正则表达式判断密码,很简单 学习了angularjs的ng-if ng-show ng-switch的区别并使用 https://www.cnblogs.com/54td/p/59743 ...

  10. 读《实战 GUI 产品的自动化测试》之:第四步,高阶技巧

    转自:http://www.ibm.com/developerworks/cn/rational/r-cn-guiautotesting4/ 定义测试控件库 本系列前几篇文章对 IBM 框架做了介绍, ...