Logic Expression Tree

题目1 : Logic Expression Tree

hiho一下 第217周

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

You are given a logic expression tree of N nodes which are numbers from 1 to N. The leaf nodes are boolean values(TRUE, FALSE) and the non-leaf nodes are logic operators(AND, OR). The value of tree is the boolean value when you calculate the expression from leaves to the root. For example below is a logic expresssion tree whose valus is TRUE AND (FALSE OR TRUE) = TRUE.

      AND
     /   \
    T    OR
        /  \
       F    T

Now you want to reverse the value of the tree (from TRUE to FALSE or vice versa). You want to know how many operators at least are needed to be reversed (from AND to OR or vice versa). For example you only need to reverse the OR operator into AND to make the tree from TRUE to FLASE.

输入

The first line contains an integer N. (1 <= N <= 100)
The i-th line of the following N lines contains an integer Pi and a string Si. 
Pi denotes the number of the i-th node's parent node. Pi = 0 indicates that the i-th node is the root. (0 <= Pi <= N)
Si is either TRUE, FALSE, AND or OR denoting the boolean value or logic operator of the node.

输出

The minimum number of operators needed to be reversed. If you cannot reverse the value no matter how many operators are reversed output -1.

样例输入

5  
0 AND  
1 TRUE   
1 OR  
3 FALSE  
3 TRUE

样例输出

1

猜你喜欢

转载自blog.csdn.net/weixin_38970751/article/details/85464354