分类:
2009-06-21 22:04:42
Time Limit:1s | Memory limit:32M |
Accepted Submit:29 | Total Submit:108 |
The binary search tree (BST) is defined in the former problem “binary search tree”
Now, also use a lot of disparate integers to build a binary search tree. But we has knew the shape of the BST, please tell me how many orders these integers could be in?
Input
The input will contain several test cases. Each test case begins with an integer N(1≤n≤1000), and N lines follows, each line contains three integers A, B, and C(the tree node ID are numbered from 1 to N), denote the node A has left children B and right children C. if B or C equals to 0,the node A’s corresponding position has no children.
The node with ID 1 is always the root of the BST.
Output
The answer mod 5087.
Sample Input
4
1 2 3
2 0 4
3 0 0
4 0 0
Sample Output
3
Hint
The ID orders can be in follow:
|