当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。
Original Problem
/* Adapted from "Help the problem setter", Ulm Local 2005 */ Let us define a binary search tree inductively as follows:
Given such a binary search tree, the following search procedure can be used to locate a node in the tree: Start with the root node. Compare the label of the current node with the desired label. If it is the same, you have found the right node. Otherwise, if the desired label is smaller, search in the left subtree, otherwise search in the right subtree. The access frequency of a node is the frequency you search for the node. The access cost to locate a node is the number of nodes you have to visit until you find the right node. Given some nodes labeled with integers and the access frequency of every node, an optimal binary search tree is a binary search tree consisting of these nodes with the minimum expected access cost. Here is your task: Given a binary search tree, try to specify the access frequency of every node, for which this binary search tree is the unique optimal binary search tree (The unique optimal binary search tree has the minimum expected access cost, which is less than any other binary search tree consisting of the same nodes with the same access frequencies). Original Problem's Input The input contains one test case. The test case starts with an integer N (1 <= N <= 50), the number of nodes in the optimal binary search tree. For simplicity, the labels of the nodes will be integers from 1 to N. The following N lines describe the structure of the tree. The i-th line contains the labels of the roots of the left and right subtrees of the node with label i (or -1 for an empty subtree). You can assume that the input always defines a valid binary search tree. Original Problem's Output For the test case, write one line containing the access frequency for each node in increasing order of the labels of the nodes. To avoid problems with floating point precision, the frequencies should be written as non-negative integers without leading zeros (meaning the access probability for a node will be the frequency divided by the sum of all frequencies). Make sure that you do not write any integer bigger than 1015. Original Problem's Sample Input 3 -1 -1 1 3 -1 -1 Original Problem's Sample Output 1 1 1 Original Problem's Hint Note that the test case in the sample input describes a tree looking like: 2 |
#Start Input# 3 -1 -1 1 3 -1 -1 #End Input# #Start Programmer's Output# 1 1 1 #End Programmer's Output# #Start Input# 10 -1 2 -1 3 -1 4 -1 5 -1 6 -1 7 -1 8 -1 9 -1 10 -1 -1 #End Input# #Start Programmer's Output# 512 256 128 64 32 16 8 4 2 89798 #End Programmer's Output# #End#
Accepted Wrong
时间上限 | 内存上限 |
1000 | 65536 |