Purifying Machine
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4014   Accepted: 1127

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

Source

 
找出所有被感染的元素,然后对两个元素满足二进制位有且仅有一个不相同的建一条边,不难证明此图是二分图,每多一条匹配边可以减少一次操作,求出最大匹配减掉即可
 
 #include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; #define maxn 2005 int n,m,len,ans;
int ele[maxn],match[maxn];
vector<int> G[maxn];
bool vis[maxn]; bool judge(int x1,int x2) {
int sum = ;
for(int i = ; i < ; ++i) {
if((x1 >> i & ) ^ (x2 >> i & )) ++sum;
} return sum == ;
}
void build() {
for(int i = ; i < len; ++i) {
for(int j = i + ; j < len; ++j) {
if(judge(ele[i],ele[j])) {
G[i].push_back(j);
G[j].push_back(i);
}
}
} } bool dfs(int u) {
for(int i = ; i < G[u].size(); ++i ) {
int v = G[u][i];
if(vis[v]) continue;
vis[v] = ;
if(match[v] == - || dfs(match[v])) {
match[v] = u;
return true;
}
} return false;
} void solve() { for(int i = ; i < len; ++i) G[i].clear();
build(); for(int i = ; i < len; ++i) {
match[i] = -;
} for(int i = ; i < len; ++i) {
memset(vis,,sizeof(vis));
if(dfs(i)) ++ans;
}
} int main()
{
// freopen("sw.in","r",stdin);
while(~scanf("%d%d",&n,&m)) {
len = ;
ans = ;
if(!n && !m) break;
char ch[];
for(int i = ; i < m; ++i) {
scanf("%s",ch);
int pos = -,sum = ;
for(int j = ; j < n; ++j) {
if(ch[j] == '')
sum += ( << (n - j - ));
if(ch[j] == '*')
pos = n - j - ;
} ele[len++] = sum;
if(pos != -) ele[len++] = sum + ( << pos); } sort(ele,ele + len);
len = unique(ele,ele + len) - ele; solve(); printf("%d\n",len - (ans / ));
} return ;
}

POJ 2724的更多相关文章

  1. poj 2724 Purifying Machinef

    poj 2724 Purifying Machinef 题意 每一个01串中最多含有一个'*','*'既可表示0也可表示1,给出一些等长的这样的01串,问最少能用多少个这样的串表示出这些串.如:000 ...

  2. POJ 2724 Purifying Machine(最大独立集)

    POJ 2724 Purifying Machine 题目链接 题意:这题题意有点没看懂.看了别人的题解, 给出m串长度为n的01串. 有些串中可能包括,这种串能够表示两个串,为1 和为0. 反复的算 ...

  3. POJ 2724 Purifying Machine (二分图匹配)

    题意 给定m个长度为n的01串(*既表示0 or 1.如*01表示001和101).现在要把这些串都删除掉,删除的方法是:①一次删除任意指定的一个:②如果有两个串仅有一个字符不同,则可以同时删除这两个 ...

  4. poj 2724 二分图最大匹配

    题意: 会给出M个串,我们要做的就是将这M个串给清除了.对于任意两个串,若二进制形式只有一位不一样,那么这两个串可以在一次操作消除,否则每个操作只能消除一个串. 3 3 *01 100 011 可以代 ...

  5. poj 2724 Purifying Machine

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

  6. TTTTTTTTTTTTTTTTTT POJ 2724 奶酪消毒机 二分匹配 建图 比较难想

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

  7. poj 2724 Purifying Machine(二分图最大匹配)

    题意: 有2^N块奶酪,编号为00...0到11..1. 有一台机器,有N个开关.每个开关可以置0或置1,或者置*.但是规定N个开关中最多只能有一个开关置*. 一旦打开机器的开关,机器将根据N个开关的 ...

  8. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  9. 图论常用算法之一 POJ图论题集【转载】

    POJ图论分类[转] 一个很不错的图论分类,非常感谢原版的作者!!!在这里分享给大家,爱好图论的ACMer不寂寞了... (很抱歉没有找到此题集整理的原创作者,感谢知情的朋友给个原创链接) POJ:h ...

随机推荐

  1. 从0 开始 WPF MVVM 企业级框架实现与说明 ---- 第三讲 WPF中 DataTemplate

    后面在我们这项目中会大量用到模板,主要指的是空间模板,数据模板会用得比较少,下面我想介绍下控件模板和数据模板,我看到有位大神写得比较不错,我整理了下,让大家能更好理解,供大家参考, 首先介绍 Data ...

  2. Mybatis 实现传入参数是表名

    <select id="totals" resultType="string"> select count(*) from ${table} < ...

  3. Redo日志

    undo日志有一个潜在的问题,即我们在将书屋改变的所有数据写到磁盘前不能提交该事务.有时,如果让数据库修改暂时只存在于主存中,我们可以节省磁盘IO;只要在崩溃发生时有日志可以恢复,这样做就是安全的. ...

  4. 学习XML总结

    XML 元素指的是从(且包括)开始标签直到(且包括)结束标签的部分. 元素可包含其他元素.文本或者两者的混合物.元素也可以拥有属性. xml包含如下: 元素 文本 属性 元素 命名: 名称可以含字母. ...

  5. Http协议[Get和Post]详解

    (2012-11-27 11:23:26) 标签: android http get post mars 分类: Android系列 访问url,需要连接网络.所以,首先应该添加Manifest权限: ...

  6. 在VS2010 SP1基础上安装mvc3

    安装VS2010 SP1后,再安装mvc3会报错,估计原因是此安装包会安装VS的补丁,而sp1的补丁版本高过此安装包的. AspNetMVC3ToolsUpdateSetup.exe 解决办法: 运行 ...

  7. poj 1338 Ugly Numbers

    原题链接:http://poj.org/problem?id=1338 优先队列的应用,如下: #include<cstdio> #include<cstdlib> #incl ...

  8. linux php安装zookeeper扩展

    linux php安装zookeeper扩展 tags:php zookeeper linux ext 前言: zookeeper提供很犀利的命名服务,并且集群操作具有原子性,所以在我的多个项目中被采 ...

  9. 条款20:以const-reference传递替换by-value传递

    缺省情况下,C++中函数参数的传递方式为by-value.即函数都是以实际参数的副本进行传递,而函数返回的也是一个副本.考虑如下实例程序: #include <iostream> clas ...

  10. com.Goods.ForEach

    com.Goods.ForEach(g => { g.TransactionPrice = getUnitPriceByProductId(g.ProductID); g.ExpressMone ...