首页 技术 正文
技术 2022年11月15日
0 收藏 797 点赞 3,864 浏览 2198 个字

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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,490
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,905
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,738
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,491
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,129
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,292