首页 技术 正文
技术 2022年11月6日
0 收藏 981 点赞 678 浏览 1969 个字

正题

题目链接:https://www.luogu.com.cn/problem/P4258


题目大意

给出\(n\)个球,\(m\)个篮筐,每个球都可以被放入一些特定的篮筐,每个球都要放,要求球的个数小于等于\(1\)的篮筐数量最多。

保证有解,输出方案。

\(1\leq T\leq 5,1\leq n\leq 3m,1\leq m\leq 100\)


解题思路

额其实做题之前已经知道正解是带花树就简单很多了。

每个篮筐我们开一个三个点的环,那么如果环上大于一个点呗匹配掉了那么这个环内就无法匹配了。

又因为一定有解所以肯定不会因为环上的匹配使答案更劣,所以这样匹配出来的结果为\(ans\)那么答案就是\(ans-n\)。

但是有一个问题我们发现这样跑的话每个球不一定都是匹配点,这样会影响我们输出方案。因为我们是找增广路的做法,这种做法不会减少已经匹配了的点,所以我们如果优先跑球代表的点就不会有问题了。


code

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N=1010,M=2e5+10;
struct node{
int to,next;
}a[M<<1];
int cnt,tot,T,n,m,e,ans,ls[N];
int dfn[N],pre[N],fa[N],match[N],tag[N];
queue<int> q;
void addl(int x,int y){
a[++tot].to=y;a[tot].next=ls[x];ls[x]=tot;
a[++tot].to=x;a[tot].next=ls[y];ls[y]=tot;
return;
}
int find(int x)
{return (fa[x]==x)?(x):(fa[x]=find(fa[x]));}
int LCA(int x,int y){
++cnt;x=find(x);y=find(y);
while(dfn[x]!=cnt){
dfn[x]=cnt;
x=find(pre[match[x]]);
if(y)swap(x,y);
}
return x;
}
void Blossom(int x,int y,int lca){
while(find(x)!=lca){
pre[x]=y;y=match[x];
if(tag[y]==2){tag[y]=1;q.push(y);}
fa[x]=fa[y]=lca;x=pre[y];
}
return;
}
int Agu(int s){
memset(tag,0,sizeof(tag));
memset(pre,0,sizeof(pre));
for(int i=1;i<=3*m+n;i++)fa[i]=i;
while(!q.empty())q.pop();
q.push(s);tag[s]=1;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=ls[x];i;i=a[i].next){
int y=a[i].to;
if(!tag[y]){
tag[y]=2;pre[y]=x;
if(!match[y]){
for(int u=y,lst;u;u=lst)
lst=match[pre[u]],match[u]=pre[u],match[pre[u]]=u;
return 1;
}
tag[match[y]]=1;q.push(match[y]);
}
else if(tag[y]==1&&find(x)!=find(y)){
int lca=LCA(x,y);
Blossom(x,y,lca);
Blossom(y,x,lca);
}
}
}
return 0;
}
int main()
{
scanf("%d",&T);
while(T--){
tot=ans=0;
memset(ls,0,sizeof(ls));
memset(match,0,sizeof(match));
scanf("%d%d%d",&n,&m,&e);
for(int i=1;i<=m;i++)
addl(i*3-2,i*3-1),addl(i*3-1,i*3),addl(i*3,i*3-2);
for(int i=1;i<=e;i++){
int x,y;
scanf("%d%d",&x,&y);
addl(x+3*m,y*3);
addl(x+3*m,y*3-1);
addl(x+3*m,y*3-2);
}
for(int i=n+3*m;i>=1;i--)
if(!match[i])ans+=Agu(i);
printf("%d\n",ans-n);
for(int i=1;i<=n;i++)
printf("%d ",1+(match[3*m+i]-1)/3);
putchar('\n');
}
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918