Submission #40342667


Source Code Expand

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define op(x) ((x&1)?x+1:x-1)
#define odd(x) (x&1)
#define even(x) (!odd(x))
#define lc(x) (x<<1)
#define rc(x) (lc(x)|1)
#define lowbit(x) (x&-x)
#define mp(x,y) make_pair(x,y)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
using namespace std;
const int MAXN=110,MAXM=4510,INF=1e9;
void tomin(int& x,int y){x=min(x,y);}
void tomax(int& x,int y){x=max(x,y);}
int n,m;
char s[MAXN][MAXN];

int r[MAXN],cnt,lim[MAXN],pcnt[1<<7];
int trans[MAXM][1<<7];

struct Node{
	int a[10];
	bool operator<(const Node& n2)const{
		rep(i,1,m)if(a[i] != n2.a[i])return a[i] < n2.a[i];
		return 0;
	}
}res[MAXM];
map<Node,int>conv; 

int dp[MAXN][MAXM],ans=INF;

void dfs(int cur,int mx){
	if(cur > m){
		Node node;
		rep(i,1,m)node.a[i]=r[i];
		conv[node]=++cnt;
		res[cnt]=node;

		return;
	}
	r[cur]=-1;
	dfs(cur+1,mx);

	r[cur]=mx+1;
	dfs(cur+1,mx+1);

	rep(i,1,mx)r[cur]=i,dfs(cur+1,mx);
}

struct DSU{
	int fa[MAXN];
	void init(int n){iota(fa+1,fa+1+n,1);}
	int find(int x){return (fa[x]==x)?(x):(fa[x]=find(fa[x]));}
	void merge(int x,int y){fa[find(x)]=find(y);}
}dsu;	

void calc(int x,int mask){
	dsu.init(2*m);
	rep(i,1,m)rep(j,i+1,m)if(res[x].a[i] == res[x].a[j] && res[x].a[i] != -1)dsu.merge(i,j);
	rep(i,1,m-1){
		if((mask>>i&1) && (mask>>(i-1)&1))dsu.merge(m+i,m+i+1);
	}
	rep(i,1,m){
		if((mask>>(i-1)&1) && res[x].a[i] != -1)dsu.merge(i,m+i);
	}

	int mx=0;
	rep(i,1,m){
		if((mask>>(i-1)&1)){
			int flag=0;
			rep(j,1,i-1)if(dsu.find(m+i) == dsu.find(m+j)){flag=1;r[i]=r[j];break;}
			if(!flag)r[i]=++mx;
		}else r[i]=-1;	
	}
	int flag=1;
	rep(i,1,m)if(res[x].a[i] != -1){
		int tmpflag=0;
		rep(j,1,m)tmpflag |= (dsu.find(i) == dsu.find(m+j) );
		flag &= tmpflag;
	}

	if(!flag)return;

	Node node;
	rep(i,1,m)node.a[i]=r[i];

	int idx=conv[node];

	trans[x][mask]=idx;
}

int check(int x){
	rep(i,1,m)if(res[x].a[i] > 1)return 0;

	return 1;
}

int main(){

	cin>>n>>m;
	rep(i,0,(1<<m)-1)pcnt[i]=__builtin_popcount(i);

	rep(i,1,n)cin>>(s[i]+1);
	rep(i,1,n)rep(j,1,m)if(s[i][j] == '#')lim[i] |= (1<<(j-1));

	int ed=0;
	per(i,n,1)if(lim[i]){ed=i;break;}

	dfs(1,0);

	rep(i,1,cnt)rep(j,0,(1<<m)-1)calc(i,j);
	
	rep(i,0,n)rep(j,1,cnt)dp[i][j]=INF;

	dp[0][1]=0;

	rep(i,0,ed-1)rep(j,1,cnt)if(dp[i][j] < INF){
		int tmp=dp[i][j];

		rep(mask,0,(1<<m)-1)if((mask & lim[i+1]) == lim[i+1]){
			tomin(dp[i+1][trans[j][mask]],tmp + pcnt[mask] - pcnt[lim[i+1]]);
		}
	}

	rep(j,1,cnt)if(check(j)){tomin(ans,dp[ed][j]);}
	cout<<ans<<endl;

    return 0;
}

Submission Info

Submission Time
Task Ex - Unite
User Crying
Language C++ (GCC 9.2.1)
Score 600
Code Size 2722 Byte
Status AC
Exec Time 229 ms
Memory 7968 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 72
Set Name Test Cases
Sample example_00.txt, example_01.txt, example_02.txt
All example_00.txt, example_01.txt, example_02.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, hand_12.txt, hand_13.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, random_35.txt, random_36.txt, random_37.txt, random_38.txt, random_39.txt, random_40.txt, random_41.txt, random_42.txt, random_43.txt, random_44.txt, random_45.txt, random_46.txt, random_47.txt, random_48.txt, random_49.txt, random_50.txt, random_51.txt, random_52.txt, random_53.txt, random_54.txt
Case Name Status Exec Time Memory
example_00.txt AC 9 ms 3576 KB
example_01.txt AC 2 ms 3512 KB
example_02.txt AC 2 ms 3608 KB
hand_00.txt AC 226 ms 7924 KB
hand_01.txt AC 226 ms 7960 KB
hand_02.txt AC 222 ms 7908 KB
hand_03.txt AC 221 ms 7960 KB
hand_04.txt AC 227 ms 7964 KB
hand_05.txt AC 27 ms 4720 KB
hand_06.txt AC 29 ms 4816 KB
hand_07.txt AC 221 ms 7968 KB
hand_08.txt AC 221 ms 7872 KB
hand_09.txt AC 2 ms 3476 KB
hand_10.txt AC 2 ms 3832 KB
hand_11.txt AC 225 ms 7964 KB
hand_12.txt AC 224 ms 7820 KB
hand_13.txt AC 228 ms 7808 KB
random_00.txt AC 220 ms 6208 KB
random_01.txt AC 220 ms 7884 KB
random_02.txt AC 226 ms 7924 KB
random_03.txt AC 30 ms 4804 KB
random_04.txt AC 28 ms 4848 KB
random_05.txt AC 2 ms 3848 KB
random_06.txt AC 32 ms 4760 KB
random_07.txt AC 226 ms 7816 KB
random_08.txt AC 28 ms 4108 KB
random_09.txt AC 24 ms 4836 KB
random_10.txt AC 23 ms 3932 KB
random_11.txt AC 223 ms 7820 KB
random_12.txt AC 229 ms 7964 KB
random_13.txt AC 27 ms 4744 KB
random_14.txt AC 222 ms 7704 KB
random_15.txt AC 2 ms 3840 KB
random_16.txt AC 28 ms 4816 KB
random_17.txt AC 223 ms 7756 KB
random_18.txt AC 222 ms 6324 KB
random_19.txt AC 224 ms 7960 KB
random_20.txt AC 27 ms 3932 KB
random_21.txt AC 221 ms 7876 KB
random_22.txt AC 224 ms 7856 KB
random_23.txt AC 221 ms 7956 KB
random_24.txt AC 222 ms 7684 KB
random_25.txt AC 2 ms 3964 KB
random_26.txt AC 223 ms 7840 KB
random_27.txt AC 221 ms 7768 KB
random_28.txt AC 29 ms 3976 KB
random_29.txt AC 29 ms 4764 KB
random_30.txt AC 220 ms 6052 KB
random_31.txt AC 225 ms 7964 KB
random_32.txt AC 226 ms 7820 KB
random_33.txt AC 30 ms 4720 KB
random_34.txt AC 24 ms 4856 KB
random_35.txt AC 2 ms 3936 KB
random_36.txt AC 30 ms 4768 KB
random_37.txt AC 26 ms 4828 KB
random_38.txt AC 218 ms 6136 KB
random_39.txt AC 227 ms 7700 KB
random_40.txt AC 25 ms 4024 KB
random_41.txt AC 28 ms 4876 KB
random_42.txt AC 26 ms 4824 KB
random_43.txt AC 25 ms 4756 KB
random_44.txt AC 29 ms 4732 KB
random_45.txt AC 2 ms 3808 KB
random_46.txt AC 225 ms 7836 KB
random_47.txt AC 222 ms 7808 KB
random_48.txt AC 27 ms 4144 KB
random_49.txt AC 222 ms 7868 KB
random_50.txt AC 219 ms 6212 KB
random_51.txt AC 222 ms 7764 KB
random_52.txt AC 222 ms 7844 KB
random_53.txt AC 31 ms 4788 KB
random_54.txt AC 24 ms 4796 KB