#P5777. [CTSC2001] 逻辑电路最优设计
[CTSC2001] 逻辑电路最优设计
Problem Description
Professor W teaches a “Digital Logic” course in the Department of Computer Science at University T, mainly about how to design logic circuits. One day, Professor W assigned a lab: design and implement a logic decoder circuit with inputs and outputs. Designing such a circuit is not difficult by itself, but the professor imposed the following requirements:
- You may only use gate components with inputs and output, and the available types and quantities of gates are given.
- Use as few gate components as possible.
These requirements stumped the whole department, so student Q came to you, who are participating in CTSC (China Team Selection Contest), and hopes you can write a program to automatically find a wiring scheme that satisfies the requirements.
In digital logic, all signals can be considered to have only two values: “high level” and “low level”, represented by and , respectively.
The behavior of a gate component is uniquely determined by its input/output truth table. A truth table describes the relationship between input signal levels and the output signal level. For example, the symbol and truth table of an “AND gate” are shown below:

| X | Y | S |
|---|---|---|
| 0 | 0 | 0 |
| 1 | ||
| 0 | 1 | |
| 1 | 1 |
In the figure above, if both inputs and of the AND gate are at high level , then the output is also high level ; otherwise, the output is low level .
Assume that all gate components provided in this lab have symmetric inputs, meaning that swapping the two inputs does not change the output. However, if an input pin of a gate is left floating (i.e., no input signal is connected), then the output is meaningless.
When wiring the circuit, the output pin of a gate may send its signal to the input pins of multiple other components, but each input pin of a gate may receive a signal from only one output pin, as shown below:

Among them, the first two wiring methods are allowed, while the last one is not allowed.
In addition, signals must be transmitted in only one direction. That is, the output of a gate cannot return (directly or indirectly through other gates) to an input of the same gate. The following figures show two forbidden wiring methods:

The decoder circuit you are required to design is a logic circuit with four inputs and four outputs. The input-output relationship of this decoder is given by a truth table, i.e., for each input combination, the states of the four output pins are specified. Clearly, there are possible input combinations. For example, a simple -input, -output decoder circuit built from the AND gate above is shown below (where A1, A2 are inputs, and Y1, Y2 are outputs):

| A1 | A2 | Y1 | Y2 |
|---|---|---|---|
| 0 | 0 | 0 | |
| 1 | |||
| 0 | 1 | ||
| 1 | 1 | ||
Input Format
The first line contains a positive integer , the number of component types. The next consecutive lines each describe one type of component. For an integer :
Line contains four integers separated by spaces: .
Here, the positive integer is the number of available components of type (where is the type index). The total number of all components will not exceed (the lab budget is limited). denotes the output when the two inputs are and , respectively. That is, are three values each being either or , representing the output when both inputs are ; when one input is and the other is ; and when both inputs are .
Lines to give the truth table of the integrated circuit to be constructed. Each line contains numbers, each either or . The first four numbers correspond to the signals on the four input pins (numbered ) in order; no two lines have exactly the same first four numbers. The last four numbers correspond to the signals that the four output pins should produce (in order) when the inputs are the given first four numbers.
Output Format
The first line contains one word, Yes or No. If there exists a design that satisfies the requirements, output Yes; otherwise output No.
If the first line is No, the output ends.
Otherwise, the second line contains one non-negative integer , the minimum number of gate components needed. The next lines each describe the wiring of one gate in the circuit. Each line contains four positive integers separated by spaces: . Here, is the index of this gate (all used gates are numbered , while are reserved for the four input pins). is the component type index (numbered in the order of the input). and are the indices of the gate or decoder input pin connected to the two input pins of this component, respectively (where ).
The last line contains four positive integers, indicating the indices of the components connected to the four output pins of the decoder circuit, respectively (each between ).
1
5 0 1 0
0 0 0 0 0 0 0 0
1 0 0 0 1 0 0 0
0 1 0 0 1 1 0 0
1 1 0 0 0 1 0 0
0 0 1 0 0 1 1 0
1 0 1 0 1 1 1 0
0 1 1 0 1 0 1 0
1 1 1 0 0 0 1 0
0 0 0 1 0 0 1 1
1 0 0 1 1 0 1 1
0 1 0 1 1 1 1 1
1 1 0 1 0 1 1 1
0 0 1 1 0 1 0 1
1 0 1 1 1 1 0 1
0 1 1 1 1 0 0 1
1 1 1 1 0 0 0 1
Yes
3
5 1 2 1
6 1 3 2
7 1 4 3
5 6 7 4
Hint
Sample Explanation
The wiring scheme corresponding to the sample is shown below:

Translated by ChatGPT 5