【高效算法设计——二分法】UVa 1607 Gates

Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

 Status

Description

Download as PDF

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

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

Example

Look at the following design.

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

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

Task

Write a program which 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 the inputs of the net,
  • writes the result.

Input 

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

Each data set consists of two consecutive lines. The rst of those lines contains exactly two 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 the number of the gates in the net.

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

Output 

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

The answer to one data set should consist of a sequence of exactly k characters terminated by the end 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' ). The i-th symbol of the sequence denotes the assignment to the i-th input of the net.

If there are more than one optimal assignment then your program should output any of them (but only one).

Sample Input 

1
3 6
-1 -3 -1 -2 1 2 1 2 4 3 5 5

Sample Output 

10x
题意:给定一个与非门电路,刚开始所有输入都是x,求把其中一些输入改成常数,用最少的x完成原来的功能
思路:输入只为x,那么输出只可能有四种情况,x,~x,0,1,,如果输出为常数即0或者1,那么我们可以把所有输入都改成0或者1,输出也固定为原来的常数,如果输出是x和~x,那么我们假设输出为x,即输入为0的时候输出也为0,这个时候我们把第一个输入改为1,其他全为0,即1000...如果此时输出变为1了,那么说明x0000..能完成原来程序的功能,因此找到一个解,如果不是,我们再把第二个数改为1,依次测试,直到找到一组解,我们可以用二分的方式来枚举1的个数
至于如何根据输入进行输出呢?这里我们使用递推的方式,开一个out数组,保存每个与非门的out值即可
代码如下:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=100000+5;
const int maxm=200000+5;
int m;
bool S[maxn];
bool gate_output[maxm];
typedef struct
{
    int in1,in2;
}NA;
NA M[maxm];


bool check()
{
    for(int i = 1; i <= m; i ++) {
        int x = M[i].in1, y = M[i].in2;
        int input1, input2;
        if(x < 0) input1 = S[-x];
        else      input1 = gate_output[x];
        if(y < 0) input2 = S[-y];
        else      input2 = gate_output[y];
        gate_output[i] =!(input1&input2);
    }
    return gate_output[m];
}

int main()
{
    int a,b,k,i,j,n;
    bool r1,r2;

    int T;

    scanf("%d",&T);
    for(int kase=1;kase<=T;kase++)
    {
        scanf("%d%d",&n,&m);
        for(j=1;j<=m;j++)
        {
            scanf("%d%d",&M[j].in1,&M[j].in2);

        }
        memset(S,false,sizeof S);
        r1=check();
        memset(S,true,sizeof S);
        r2=check();
        if(r1^r2)
        {
            int l,r;
            l=0;
            r=n-1;
            while(l < r)
            {
                int mid = (l+r)>>1;
                memset(S, false, sizeof(S));
                memset(S, true, (mid+2) * sizeof(bool));

                if(check()^r1)
                {
                    r=mid;
                }

                else
                {
                    l=mid+1;
                }

            }

           for(i=1;i<=l;i++)
               printf("1");

           printf("x");

           for(i=l+1;i<n;i++)
                printf("0");

           puts("");

        }
        else
        {
            for(i=0;i<n;i++)
                printf("0");
            puts("");
        }
    }


    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值