Codeforces Round 402 (Div. 1)
B
Bitwise Formula
Codeforces
1800
原题
← AC →
翻译中…

Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game.

翻译中…

Initially, Bob chooses integer $*m*$, bit depth of the game, which means that all numbers in the game will consist of $*m*$ bits. Then he asks Peter to choose some $*m*$-bit number. After that, Bob computes the values of $*n*$ variables. Each variable is assigned either a constant $*m*$-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values.

翻译中…

Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose.

翻译中…

Input

翻译中…

The first line contains two integers $*n*$ and $*m*$, the number of variables and bit depth, respectively ($1 ≤ *n* ≤ 5000$; $1 ≤ *m* ≤ 1000$).

翻译中…

The following $*n*$ lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of:

翻译中…
  1. Binary number of exactly $*m*$ bits.
  2. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter.
翻译中…

Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different.

翻译中…

Output

翻译中…

In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as $*m*$-bit binary numbers.

翻译中…

Note

翻译中…

In the first sample if Peter chooses a number $011_{2}$, then $*a* = 101_{2}, *b* = 011_{2}, *c* = 000_{2}$, the sum of their values is $8$. If he chooses the number $100_{2}$, then $*a* = 101_{2}, *b* = 011_{2}, *c* = 111_{2}$, the sum of their values is $15$.

翻译中…

For the second test, the minimum and maximum sum of variables $*a*$, $*bb*$, $*cx*$, $*d*$ and $*e*$ is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is $0$.

翻译中…

样例

Input #1
3 3
a := 101
b := 011
c := ? XOR b
Output #1
011
100
InputCopy OutputCopy
Input #2
5 1
a := 1
bb := 0
cx := ? OR a
d := ? XOR ?
e := d AND bb
Output #2
0
0
InputCopy OutputCopy
题目信息
题号
778B
时间限制
3 s
内存限制
512 MB
标签
位运算
暴力
DFS 及类似
表达式解析
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。