The decrease in the number of template collections is due to matching pairs of strings that are not matched in the string collection. So just find the maximum match for a binary graph.
The code does not explicitly create a binary diagram, you can imagine two sets X and Y, because two sets will run again, so a match will be counted two times, the return time divided by 2 on the line.
Pick up the Hungarian algorithm that almost forgot ...
#include <bits/stdc++.h>using namespacestd;Const intMAXN =Ten;Const intN =1<<MAXN;#defineDebug (x) \Bitset< ->BS (x); cout<<bs<<Endl;BOOLVis[n];intMatch[n];intDfst[n], Dfstime, N, M, M,tot;BOOLDfsintu) { for(inti =0; I < n; i++){ intv = u^ (1<<i); if(Vis[v] && dfst[v]! =dfstime) {Dfst[v]=Dfstime; if(!~match[v] | |DFS (Match[v])) {Match[v]=u; return true; } } } return false;}intMaxmatch () {memset (match,-1,sizeof(int)*M); memset (DFST,0,sizeof(int)*M); Dfstime=0; intAns =0; for(inti =0; i < M; i++){ if(Vis[i]) {dfstime++; if(Dfs (i)) ans++; } } returnAns>>1;}intRead () {M=1<<N; memset (Vis,0,sizeof(BOOL)*M); for(inti =0; I < m; i++){ intFG =-1, t =0; scanf ("\ n"); Charch; for(intj =0; J < N; J + +) {ch=GetChar (); if(ch = ='*') FG =J; if(ch! ='0') T |=1<<J; } Vis[t]=true; if(~FG) vis[t^ (1<<FG)] =true; } intRET =0; for(inti =0; i < M; i++)if(Vis[i]) ret++; returnret;}intMain () {//freopen ("In.txt", "R", stdin); while(SCANF ("%d%d",&n,&m), N) {printf ("%d\n", read ()-Maxmatch ()); } return 0;}
UVA1663 Purifying Machine