Purifying Machine
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5004   Accepted: 1444

Description

Mike is the owner of a cheese factory. He has 2N cheeses and each cheese is given a binary number from 00...0 to 11...1. To keep his cheese free from viruses, he made himself a purifying machine to clean virus-infected cheese. As a talented programmer, his purifying machine is built in a special way. His purifying machine has N switches, each switch has three states, 1, 0 and *. An operation of this machine is a cleaning action according to the states of the N switches. During one operation, at most one switch can be turned to state *, which can substitute for either 1 or 0. When the machine is turned to a specific state, an operation will clean all the cheeses with corresponding binary numbers. For example, if N equals 6 and the switches are turned to 01*100, the cheeses numbered 010100 and 011100 are under operation by the machine.

One day, Mike's machine was infected. When Mike found out, he had already done some operations and the cheeses operated by this infected machine were infected too. He cleaned his machine as quickly as he could, and now he needs to clean the infected cheeses with the minimum number of operations. If a cheese is infected, cleaning this cheese with the machine one or more times will make this cheese free from virus again; but if a cheese is not infected, operation on this cheese will make it go bad.

Now given the infected operations Mike has done, you need to find out the minimum number of operations that must be performed to clean all the infected cheeses without making any clean cheese go bad.

Input

There are several test cases. Each test case starts with a line containing two numbers N and M (1 <= N <= 10, 1 <= M <= 1000). N is the number of switches in the machine and M is the number of infected operations Mike has done. Each of the following M lines contains a switch state of the machine. A test case with N = M = 0 ends the input and should not be processed.

Output

For each test case, output one line containing an integer, which is the minimum number of operations Mike needs to do.

Sample Input

3 3
*01
100
011
0 0

Sample Output


Source

题意:
有2^n个奶酪,对应二进制的数,用清理机输入一个二进制数可以清理对应的奶酪,含有*的算成0和1两个,每次只能出现一个*,所以每次出现*时,能同时清除两个只有一位(*在的位)不同的二进制数,现在清理机自身感染细菌,它清理的奶酪都被感染,将清理机消毒后,问最少清理几次能把所有感染的奶酪清理干净
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <vector>
#define MM(a,b) memset(a,b,sizeof(a))
using namespace std; vector<int> G[2200];
int match[2200],used[2200];
int g,b,m,cnt,l,r;
int mp[2200],posi[2200]; void add_edge(int u,int v)
{
G[u].push_back(v);
G[v].push_back(u);
} bool dfs(int u)
{
used[u]=1;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
int w=match[v];
if(w<0||!used[w]&&dfs(w))
{
match[u]=v;
match[v]=u;
return true;
}
}
return false;
} int bipartite_match()
{
MM(match,-1);
int res=0;
for(int i=1;i<=1000+r;i++)
if(match[i]<0)
{
memset(used,0,sizeof(used));
if(dfs(i)) res++;
}
return res;
} bool onlyonedifer(int i,int j)
{
int c=(i^j);
return (c&&((c&(c-1))==0));
}//处理i,j,两个数字二进制表示只有一位不同模板 bool jione(int i)
{
int j=0;
while(i)
{
if(i&1) j++;
i>>=1;
}
return j%2==1;
} void build()
{
l=0;r=0;
for(int i=1;i<=cnt;i++)
{ if(jione(mp[i]))
{
l++;
posi[l]=mp[i];
}
else
{
r++;
posi[1000+r]=mp[i];
}
} for(int i=1;i<=1000+r;i++) G[i].clear(); for(int i=1;i<=l;i++)
for(int j=1;j<=r;j++)
if(onlyonedifer(posi[i],posi[j+1000]))
add_edge(i,j+1000);
} char s[15];
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m)&&(n||m))
{
cnt=0;
MM(mp,0);
for(int i=1;i<=m;i++)
{
scanf("%s",s);
int flag=0;
++cnt;
for(int j=0;j<n;j++)
{
if(s[j]=='*')
{
flag=j+1;
mp[cnt]=(mp[cnt]<<1)+0;
}
else
mp[cnt]=(mp[cnt]<<1)+s[j]-'0';
}
if(flag)
{
mp[cnt+1]=(mp[cnt]|(1<<(n-1-(flag-1))));
++cnt;
}
}
sort(mp+1,mp+cnt+1);//unique处理前先排序
cnt=unique(mp+1,mp+cnt+1)-(mp+1);//去重
build();
int w=bipartite_match();
printf("%d\n",w+cnt-2*w);
}
return 0;
}

  分析:细节很多的一道题;

1.处理两个数字二进制表示只有一位不同的模板,见代码

2.出现*时,进行二分匹配,建图很有技巧,将1出现个数为奇数次的放左边,偶数次的放右边

3.要进行去重处理

4.&运算符的优先级很低

 

TTTTTTTTTTTTTTTTTT POJ 2724 奶酪消毒机 二分匹配 建图 比较难想的更多相关文章

  1. TTTTTTTTTTTTTTTTT POJ 2226 草地覆木板 二分匹配 建图

    Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9754   Accepted: 3618 Desc ...

  2. HDU 3036 Escape 网格图多人逃生 网络流||二分匹配 建图技巧

    题意: 每一个' . '有一个姑娘, E是出口,'.'是空地 , 'X' 是墙. 每秒钟每一个姑娘能够走一步(上下左右) 每秒钟每一个出口仅仅能出去一个人 给定n*m的地图, 时限T 问全部姑娘是否能 ...

  3. poj 2060 Taxi Cab Scheme (二分匹配)

    Taxi Cab Scheme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5710   Accepted: 2393 D ...

  4. POJ 3020 Antenna Placement【二分匹配——最小路径覆盖】

    链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  5. poj 1034 The dog task (二分匹配)

    The dog task Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2559   Accepted: 1038   Sp ...

  6. POJ 1466 大学谈恋爱 二分匹配变形 最大独立集

    Girls and Boys Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 11694   Accepted: 5230 D ...

  7. POJ 1386 Play on Words(单词建图+欧拉通(回)路路判断)

    题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...

  8. POJ 2226 Muddy Fields 二分图(难点在于建图)

    题意:给定一个矩阵和它的N行M列,其中有一些地方有水,现在有一些长度任意,宽为1的木板,要求在板不跨越草,用一些木板盖住这些有水的地方,问至少需要几块板子? 思路:首先想到如果没有不准跨越草的条件则跟 ...

  9. hdu3715 二分+2sat+建图

    题意:       给你一个递归公式,每多一层就多一个限制,问你最多能递归多少层. 思路:      先分析每一层的限制 x[a[i]] + x[b[i]] != c[i],这里面x[] = 0,1, ...

随机推荐

  1. p1000 A+B问题

    题目描述 Description 输入两个整数A和B,输出他们的和 输入描述 Input Description 输入为一行,包含两个整数A,B.数据保证A与B都在2^31-1的范围内 输出描述 Ou ...

  2. 用python操作mysql数据库

    数据库的安装和连接 PyMySQL的安装 pip install PyMySQL python连接数据库 import pymysql db = pymysql.connect("数据库ip ...

  3. 从入门到自闭之Python闭包

    闭包 定义:在嵌套函数内,使用(非本层变量)非全局变量就是闭包 闭包必须是内层函数对外层函数的变量(非全局变量)的引用 函数执行完毕后,函数体内的空间自行销毁 def func(): a=1 def ...

  4. PYQT5 pyinstaller 打包工程

    win+R 输入cmd  回车 首先安装 pyinstaller : pip install pyinstaller 安装 pywin32: pip install pywin32 在cmd中输入工程 ...

  5. java.util.MissingFormatArgumentException: Format specifier '%s'

    java.util.MissingFormatArgumentException: Format specifier '%s' at java.util.Formatter.format(Format ...

  6. try catch和finally

    在C#中这三个关键字用于处理异常. 这三个关键字try是必定要用的,要不然就失去了意义.然后catch和finally可以不用但是要注意遵循原则. 存在一个或多个catch的时可以不用finally, ...

  7. golang日志框架--logrus学习笔记

    golang日志框架--logrus学习笔记 golang标准库的日志框架非常简单,仅仅提供了print,panic和fatal三个函数,对于更精细的日志级别.日志文件分割以及日志分发等方面并没有提供 ...

  8. js之运算符(算术运算符)

    Javascript中的运算符大多是由标点符号少数由关键字表示.可以根据其操作数的个数进行分类.大多数运算符是一个二元运算符,将两个表达式合成一个比较复杂的表达式.还有需要注意的一点是运算符的优先级, ...

  9. TableView 键盘弹起冲突

    1.TableView 上cell 带有 TextField,如果 是Plain 形式的TableView ,并且设置SectionHeader的 取消粘滞效果 会导致键盘弹起,页面不能正常 上移 问 ...

  10. idea自动抽取变量快捷键设置

    file---setting---keymap---搜索variable 如下图:默认是ctrl+alt+v,这里修改成自己比较方便的快捷键即可,我这里设置的是alt+e