Purifying Machine
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5408   Accepted: 1575

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

2

翻译:mike搞了一台机器专门清洗中毒的奶酪,有一天,机器也中毒了并且开始自顾自的清洗奶酪,被中毒的机器清洗的奶酪也会中毒,mike即时发现了这个问题,修复机器后,想要以最少的操作数,让之前中毒的奶酪回复正常。
思路:如果一个操作带有‘*’,那么机器将会同时清理两个奶酪,那么在最后清理奶酪时,尽量使用带'*'号的操作可以更快的清理完奶酪,若我们建图,并且让中毒奶酪的编号相差1的奶酪连一条边,这些连边的两块奶酪使用‘*’操作来清理1次就行,这相当于找一个最大独立集,集合中每两块奶酪都没有连边,若把集合中的奶酪全部清理了,就相当于把所有中毒奶酪都清洗了;
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<queue>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#include<bitset>
using namespace std;
#define INF 0x3f3f3f3f
const int N_MAX = ;
int V;//点的个数
vector<int>G[N_MAX];
int match[N_MAX];
bool used[N_MAX];
void add_edge(int u, int v) {
G[u].push_back(v);
G[v].push_back(u);
} bool dfs(int v) {
used[v] = true;
for (int i = ; i < G[v].size(); i++) {
int u = G[v][i], w = match[u];
if (w < || !used[w] && dfs(w)) {
match[v] = u;
match[u] = v;
return true;
}
}
return false;
} int bipartite_matching() {
int res = ;
memset(match, -, sizeof(match));
for (int v = ; v < V; v++) {
if (match[v] < ) {
memset(used, , sizeof(used));
if (dfs(v))
res++;
}
}
return res;
} int N, M;
string s[N_MAX];
vector<int>operation;
int main() {
while (scanf("%d%d",&N,&M)&&N) {
//operation.resize(N);
for (int i = ; i < M;i++)
cin >> s[i];
for (int i = ; i < M; i++) {
int x=;
for (int j = ; j < s[i].size();j++) {
if(s[i][j]=='')x += << (N - j - );
}
operation.push_back(x);
for (int j = ; j < s[i].size();j++) {
if (s[i][j] == '*')x += << (N - j - );
}
operation.push_back(x);
}
sort(operation.begin(),operation.end());
operation.erase(unique(operation.begin(),operation.end()),operation.end());
V = operation.size();
for (int i = ; i < V;i++) {
for (int j = i + ; j < V;j++) {
bitset<>bit = operation[i] ^ operation[j];//找找二进制有几位是一样的
if (bit.count() == ) {//只有一位是一样的,可以让机器使用'*'操作,符合连边条件
add_edge(i, j);
}
}
}
printf("%d\n",V-bipartite_matching());
for (int i = ; i < V;i++) {
G[i].clear();
}
operation.clear();
}
return ;
}

最新文章

  1. 【Tip】如何让引用的dll随附的xml注释文档、pdb调试库等文件不出现在项目输出目录中
  2. html5 datalist自动完成
  3. python3,交互模式,无法使用ctrl和方向键,需要和ctrl一块用
  4. Javascript 方法大全
  5. 使用EXECUTE IMMEDIATE来生成含有绑定变量的SQL
  6. VPS,虚拟主机,云主机,独立服务器区别
  7. 为什么所有浏览器的userAgent都带Mozilla
  8. Linux(gnu)环境动态链接库的搜索路径
  9. 枚举与define的区别
  10. 作为Qt 合作伙伴的V-Play,比大家都领先了一步 planet.qt.io
  11. 网站生产app的一些网址
  12. 使用dom4j解析XML例子
  13. BZOJ_3038_上帝造题的七分钟2_线段树
  14. [转]Oracle left join \ right join
  15. java web中的异常处理
  16. Eruda 一个被人遗忘的调试神器
  17. 关于初级dp的一些记忆
  18. ubuntu终端下快捷键之--字体放大缩小
  19. c++实验6 递归
  20. java POi excel 写入大批量数据

热门文章

  1. python之生成器的初识
  2. extranuclear gene|non-Mendelian inheritance|uniparental inheritance|maternal inheritance
  3. python-判断alter是否存在
  4. 监控电脑CPU,内存,文件大小,硬盘空间,IP,用户名
  5. 什么是静态代码块?java中如何使用空参构造方法自动生成不同名字的对象,使用非静态的属性和静态属性有什么区别,原因是什么?如何理解static关键字
  6. 2d游戏中的射线与矩形检测碰撞
  7. 从屏幕截取一块区域,将其赋给imageView
  8. .NET 中,编译器直接支持的数据类型称为基元类型(primitive type).基元类型和.NET框架类型(FCL)中的类型有直接的映射关系.
  9. NOIP2016——一个逗号引发的血案
  10. Voyager的安装及配置文件