시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 512 MB555100.000%

문제

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.

입력

The first line contains two integers $n$ and $m$, the number of variables and bit depth, respectively ($1 \le n \le 5000$; $1 \le m \le 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.

출력

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.

서브태스크

번호배점제한
133

$n \le 30$, $m \le 15$

233

$n \le 5000$, $m \le 1000$, only AND and OR

334

$n \le 5000$, $m \le 1000$

예제 입력 1

3 3
a := 101
b := 011
c := ? XOR b

예제 출력 1

011
100

예제 입력 2

5 1
a := 1
bb := 0
cx := ? OR a
d := ? XOR ?
e := d AND bb

예제 출력 2

0
0

채점 및 기타 정보

  • 예제는 채점하지 않는다.