1607 - Gates

来源:互联网 发布:文明5 mac 下载 编辑:程序博客网 时间:2024/05/05 19:14

In contemporaryVLSI chip industry, the software tools used by electrical engineers performmany optimizations. Your task is to implement one specific optimization of somechip design. Your tool is given an acyclic net of NAND gates (NAND gatecomputes the negated conjunction of its inputs, i.e. the output value of thegate is 0 if and only if its both input values are 1). The net is a part ofalready synthesized component and cannot be changed. All the inputs of the netare connected to one signal x. The objective is to disconnect x from someinputs and to assign constant signals 0 and/or 1 to those inputs in such a waythat the function implemented by the design remains unchanged.

We say that anassignment of x's and/or 0's and/or 1's to the inputs of the net is optimal ifthe number of inputs connected to x is the smallest possible but the net stillcomputes the same function as if all the inputs were connected to x.

Example

Look at thefollowing design.

We can change itto the design with only one variable input, for example:

(Observe thatthere are other ways of connecting the inputs to just one x and to some numberof 0's and 1's that implement the same function).

Task

Write a programwhich for each data set:

 reads the description of the net,

 computes an optimal assignment of x's and/or 0's and/or 1's to theinputs of the net,

 writes the result.

Input 

The first line ofthe input contains exactly one positive integer d equal to the number of datasets, 1 ≤ d ≤ 20. The data sets follow.

Each data setconsists of two consecutive lines. The first of those lines contains exactlytwo positive integers n and m separated by single space, 1 ≤ n ≤ 100.000, 1 ≤ m≤ 200.000. Integer n is the number of the net inputs and integer m is thenumber of the gates in the net.

The second ofthose lines contains exactly 2m nonzero integers, separated by single spaces.The numbers on positions 2j - 1 and 2j describe the signal sources for theinputs to gate j. The positive number s means the output of gate s. Thenegative number s means the (-s)-th input to the net. The gates and the netinputs are numbered starting from one. The input of each gate is connected toan input of the net or to an output of a gate whose description occurredearlier in the sequence. Each net input is connected to at least one gateinput. Each gate output is connected to at least one gate input except theoutput of the last gate that is connected to the output of the net.

Output 

The output shouldconsist of exactly d lines, one line for each data set. The line number ishould contain the answer to the i-th data set.

The answer to onedata set should consist of a sequence of exactly k characters terminated by theend of line (with no spaces in between). Each of those characters should be 0(the digit `zero' ) or 1 (the digit `one') or x (lower-case letter `x' ). Thei-th symbol of the sequence denotes the assignment to the i-th input of thenet.

If there are morethan one optimal assignment then your program should output any of them (butonly one).

Sample Input 

1

3 6

-1 -3 -1 -2 1 2 12 4 3 5 5

Sample Output 

10x

题意:给出一个与非门电路,在所有输入都为x的情况下,该电路会输出一个结果,让你将一些输入固定为常数,让x最少的情况下完成相同功能。

分析:输入只有一个值,那么整个电路只可能有四种功能·,常数0,常数1,x,非x。首先如果我们输入0和1得到相同的结果,说明电路为常数电路,任意输出即可(全0,全1)。如果输出不一样,说明电路功能为x或非x,一定有一种情况在只有1个x就完成该功能。

 

给出证明,我们已知输入全0,即111……1(始态)时输出和000……0(终态)时不同,那么11111……1,01111……1,00111……1,00011……1,-------,00000……0渐变过程一定存在这种情况…1…--->…0…的过程中输出由始态转变为终态,即为所求解(因为该过程可逆,…0…--->…1…输出同样改变)。

 

解法就是这样了,但还有一点要注意,m太大了,耗时可能很多,用二分枚举位置可以求出其中一个可行解,时间消耗也不大。

代码:

 

代码:

#include<cstdio>

#include<algorithm>

using namespacestd;

 

const int maxm =200000 + 5;

int n, m;

 

struct Gates

{

    int a, b, o;

} gates[maxm];

 

// returns theoutput of input 000..0111...1 (there are k 0's)

int output(int k)

{

    for(int i = 1; i <= m; i++)

    {

        int a = gates[i].a;

        int b = gates[i].b;

        int va = a < 0 ? -a > k :gates[a].o;

        int vb = b < 0 ? -b > k :gates[b].o;

        gates[i].o = !(va && vb);

    }

    return gates[m].o;

}

 

// returns k suchthat

// 1. output(k) =output(n)

// 2. output(k-1)= output(0)

int solve(int vn)

{

    int L = 1, R = n;

    while(L < R)

    {

        int M = L + (R-L)/2;

        if(output(M) == vn)

        {

            R = M;

        }

        else

        {

            L = M+1;

        }

    }

    return L;

}

 

int main()

{

    int T;

    scanf("%d", &T);

    while(T--)

    {

        scanf("%d%d", &n,&m);

        for (int i = 1; i <= m; i++)

        {

            scanf("%d%d",&gates[i].a, &gates[i].b);

        }

        int v0 = output(0);

        int vn = output(n);

        if(v0 == vn)

        {

            for(int i = 1; i <= n; i++)

            {

                printf("0");

            }

        }

        else

        {

            int x = solve(vn);

            for(int i = 1; i < x; i++)

            {

                printf("0");

            }

            printf("x");

            for(int i = x+1; i <= n; i++)

            {

                printf("1");

            }

        }

        printf("\n");

    }

    return 0;

}

0 0
原创粉丝点击