萌新初学二分图,求助WA
查看原帖
萌新初学二分图,求助WA
448884
快乐的大童楼主2023/1/30 19:14

RT,没测过,但在其他渠道提交是 WA 的:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define popcount __builtin_popcount
using namespace std;
inline int R(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}return x*f;
}
inline void write(int x){
	if(x<0){x=-x;putchar('-');}
	int y=0;char z[70];
	while(x||!y){z[y++]=x%10+48;x/=10;}
	while(y--)putchar(z[y]);
}
inline void writesp(int x){
	write(x);putchar(32);
}
inline void writeln(int x){
	write(x);putchar(10);
}
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define rep(a,b,c) for(int a=b;a<=c;a++)
#define per(a,b,c) for(int a=b;a>=c;a--)
#define reprange(a,b,c,d) for(int a=b;a<=c;a+=d)
#define perrange(a,b,c,d) for(int a=b;a>=c;a-=d)
#define graph(i,j,k) for(int i=head[j];i;i=k[i].nxt)
const int M=15,maxn=2e3+5,maxm=4e6+5;
int n,m;
char s[M];
struct edge{
	int to,nxt;
}a[maxm];
int head[maxn],edges;
void add(int x,int y){
	a[++edges]=(edge){y,head[x]};
	head[x]=edges;
}
int match[maxn],vis[maxn];
bool ap[maxn];
bool dfs(int x,int tag){
	if(vis[x]==tag) return 0;
	vis[x]=tag;
	graph(i,x,a){
		int u=a[i].to;
		if(!match[u]||dfs(match[u],tag)){
			match[u]=x;
			return 1;
		}
	}
	return 0;
}
void init(){
	edges=0;
	rep(i,0,(1<<n)-1) ap[i]=head[i]=vis[i]=match[i]=0;
}
void solve(){
	init();
	rep(_,1,m){
		scanf("%s",s+1);
		int num=0,pos=0;
		rep(i,1,n){
			if(s[i]=='*') pos=i;
			else num+=(1<<(i-1))*(s[i]-'0');
		}
		if(!pos) ap[num]=1;
		else ap[num]=ap[num+(1<<(pos-1))]=1;
	}
	int sum=0;
	rep(i,0,(1<<n)-1){
		if(ap[i]){
			sum++;
			rep(j,1,n){
				int num=i^(1<<(j-1));
				if(ap[num]) add(i,num);
			}
		}
	}
	int ans=0;
	rep(i,0,(1<<n)-1){
		if(dfs(i,i)){
			ans++;
		}
	}
	writeln(sum-ans/2);
}
int main(){
	while((n=R())&&(m=R())) solve();
}
2023/1/30 19:14
加载中...