POJ 2724 Purifying Machine

Source: Internet
Author: User
Tags bitset switches

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 are 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 was built in a special the. His purifying machine had N switches, each switch had three states, 1, 0 and *. An operation of a cleaning action according to the states of the N switches. During one operation, at the most one switch can is turned to state *, which can substitute for either 1 or 0. When the machine was turned to a specific state, an operation would clean all the cheeses with corresponding binary numbers. For example, if N equals 6 and the switches is turned to 01*100, the cheeses numbered 010100 and 011100 is under Operat Ion by the machine.

One day, Mike's machine was infected. When Mike found out, he had already do some operations and the cheeses operated by this infected machine were infected t Oo. He cleaned he machine as quickly as he could, and now he needs to clean the infected cheeses with the minimum number of O Perations. If A cheese is infected, cleaning this cheese with the machine one or more times would make this cheese free from virus aga In But if a cheese isn't infected, operation on this cheese would make it go bad.

Now given the infected operations Mike have done and you need to find out the minimum number of operations that must be perfor Med to clean all the infected cheeses without making no clean cheese go bad.

Input

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

Output

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

Sample Input

3 3*011000110 0

Sample Output

2

Translator: Mike had a machine dedicated to cleaning the poisoned cheese, one day, the machine was poisoned and began to clean the cheese, poisoned machine cleaning cheese will be poisoned, Mike immediately discovered the problem, repair the machine, want to use the fewest number of operations, so that the former poisoned cheese back to normal.
Idea: If an operation with ' * ', then the machine will clean up two cheese at the same time, then in the final cleaning of the cheese, try to use the "*" Operation can be faster to clean up the cheese, if we build, and let the Poison cheese number 1 of the cheese with a side, these two pieces of cheese with the side of the "* Operation to clean 1 times on the line, this is equivalent to find a maximum independent set, the collection of every two pieces of cheese has no edge, if the collection of cheese all cleaned up, it is equivalent to all the poisoned cheese is washed;
AC Code:
#define_crt_secure_no_deprecate#include<iostream>#include<algorithm>#include<queue>#include<Set>#include<vector>#include<cstring>#include<string>#include<bitset>using namespacestd;#defineINF 0x3f3f3f3fConst intN_max =1100;intV//Number of pointsvector<int>G[n_max];intMatch[n_max];BOOLUsed[n_max];voidAdd_edge (intUintv) {g[u].push_back (v); G[v].push_back (u);}BOOLDfsintv) {Used[v]=true;  for(inti =0; I < g[v].size (); i++) {        intU = g[v][i], W =Match[u]; if(W <0|| !USED[W] &&DFS (W)) {Match[v]=u; Match[u]=v; return true; }    }    return false;}intbipartite_matching () {intres =0; memset (Match,-1,sizeof(match));  for(intv =0; v < V; v++) {        if(Match[v] <0) {memset (used,0,sizeof(used)); if(Dfs (v)) Res++; }    }    returnRes;}intN, M;stringS[n_max];vector<int>operation;intMain () { while(SCANF ("%d%d", &n,&m) &&N) {//operation.resize (N);         for(inti =0; I < m;i++) Cin>>S[i];  for(inti =0; i < M; i++) {            intx=0;  for(intj =0; J < S[i].size (); j + +) {                if(s[i][j]=='1') x + =1<< (N-j-1);            } operation.push_back (x);  for(intj =0; J < S[i].size (); j + +) {                if(S[i][j] = ='*') x + =1<< (N-j-1);        } operation.push_back (x);        } sort (Operation.begin (), Operation.end ());        Operation.erase (Unique (Operation.begin (), Operation.end ()), Operation.end ()); V=operation.size ();  for(inti =0; I < v;i++) {             for(intj = i +1; J < v;j++) {Bitset< +>bit = Operation[i] ^ operation[j];//look for the binary.                if(Bit.count () = =1) {//only one is the same, allowing the machine to operate with a ' * ' to match the edge conditionAdd_edge (i, j); }}} printf ("%d\n", V-bipartite_matching ());  for(inti =0; I < v;i++) {g[i].clear ();    } operation.clear (); }    return 0;}

POJ 2724 Purifying Machine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.