POJ2724 purifying Machine "The smallest side overlay of the binary graph"

Source: Internet
Author: User

Topic Links:

http://poj.org/problem?id=2724


Main topic:

There is a 2^n cheese, numbered from 000 ... 00 to 111 ... 11, there is now a machine has n switches, each switch has a state of 3,

respectively, ' 0 ', ' 1 ', ' * ', each switch can only have one state exists. The ' * ' state can replace the ' 0 ' or ' 1 ' state. Like 11*1,

Corresponds to 1111 or 1101. Now there are M cheese infected, and each cheese is numbered with a binary string of length n.

like a switch, each one can be ' * ' except for ' 1 ', ' 0 ', which means that it can be ' 0 ', can also be ' 1 '. For example

1*1, which can be either 101 or 111. Now it's time to remove the infected cheese (binary string). Delete

of the There are two ways of doing this:

1) Remove the specified string by using the same machine as the infected cheese number once, for example, delete 111 with 111.

2) If there are two strings with only one character, you can delete both strings at once with the string ' * ' (where the package

(including duplicate strings), and Delete 101 and 111 at the same time as said with 1*1.

Every time the machine is deleted, it is converted to a state and then deleted again. Q: The minimum number of times a machine must be converted to

All strings are deleted.


Ideas:

Convert all binary strings to a binary string containing only 01, and then use the array num[] to store the binary string corresponding decimal

Number, say 101 corresponds to decimal is 5. The num[] array is then sorted to delete all duplicates. This way, you can

use a dot to represent a string. Suppose there is a cnt-no-duplicates point (binary string). Then, a binary graph is created and each side is a CNT

Point. For all two points that satisfy only one bit in the binary form, the sides are built in two directions. Finally, the problem becomes: to find the smallest of two graphs

side overwrite the problem. The minimum side coverage of the dichotomy = number of CNT-two graph maximum match/2. Using Hungarian algorithm to find the maximum matching of binary graph

Can.

for judging whether the binary form of a and B is only one bit, you can t = a^b, then judge (T && (t& (t-1)) ==0),

Satisfied, the binary form of a and B is only one bit.


AC Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int maxn = 2050;bool map[maxn][maxn],mask[maxn];int nx,ny;int cx[maxn],cy[maxn];int FindPath (int u) {for (int i = 1; I <= NY; ++i) {if (Map[u][i] &&!            Mask[i]) {mask[i] = 1; if (cy[i] = =-1 | |                Findpath (Cy[i])) {Cy[i] = u;                Cx[u] = i;            return 1; }}} return 0;}    int Maxmatch () {for (int i = 1; I <= NX; ++i) cx[i] = 1;    for (int i = 1; I <= NY; ++i) cy[i] = 1;    int res = 0;                for (int i = 1; I <= NX; ++i) {if (cx[i] = = 1) {for (int j = 1; j <= NY; ++j)            MASK[J] = 0;        Res + = Findpath (i); }} return res;}    Char s[12];int num[maxn];int main () {int n,m; while (~SCANF ("%d%d", &n,&m) && (n| |  M)) {memset (map,0,sizeof (MAP));      memset (num,0,sizeof (Num));        int cnt = 0,K;            for (int i = 1; I <= M; ++i) {scanf ("%s", s);            cnt++;            K =-1;                for (int j = 0; j < N; ++j) {if (s[j] = = ' * ') k = j;            else num[cnt] |= ((s[j]-' 0 ') <<j);                } if (k! =-1) {cnt++; NUM[CNT] = (Num[cnt-1] |            (1 << k));        }} sort (num+1,num+1+cnt);        Num[0] =-1;        int j = 0;        for (int i = 1; I <= cnt; ++i) if (num[j]! = Num[i]) num[++j] = Num[i];        CNT = j; for (int i = 1; I <= cnt, ++i) {for (int j = i+1; J <= cnt; ++j) {int T                = Num[i]^num[j];            if (t && (t& (t-1)) ==0) map[i][j] = map[j][i] = 1;        }} NX = NY = cnt; int Max = Maxmatch();    printf ("%d\n", CNT-MAX/2); } return 0;}


POJ2724 purifying Machine "The smallest side overlay of the binary graph"

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.