当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

2477:Help the problem setter

Special Judge 特殊评判
题目描述
Preparing a problem for a programming contest takes a lot of time. Not only do you have to write the problem description and write a solution, but you also have to create difficult input files. In this problem, you get the chance to help the problem setter to create some input for a certain problem.
For this purpose, let us select the problem which was not solved during last year's local contest. The problem was about finding the optimal binary search tree, given the probabilities that certain nodes are accessed. Your job will be: given the desired optimal binary search tree, find some access probabilities for which this binary search tree is the unique optimal binary search tree. Don't worry if you have not read last year's problem, all required definitions are provided in the following.
Let us define a binary search tree inductively as follows:
  • The empty tree which has no node at all is a binary search tree
  • Each non-empty binary search tree has a root, which is a node labelled with an integer, and two binary search trees as left and right subtree of the root
  • A left subtree contains no node with a label >= than the label of the root
  • A right subtree contains no node with a label <= than the label of the root

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 cost to locate a node is the number of nodes you have to visit until you find the right node. An optimal binary search tree is a binary search tree with the minimum expected access cost.
输入解释
The input contains several test cases. Each 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 subtree 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.
The last test case is followed by a zero.
输出解释
For each 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 integers, 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 263 - 1 (the maximum value fitting in the GCC/G++ data type long long , the Java data type long or VC data type __int64). Otherwise, you may produce any solution ensuring that there is exactly one optimal binary search tree: the binary search tree given in the input.
输入样例
3
-1 -1
1 3
-1 -1
10
-1 2
-1 3
-1 4
-1 5
-1 6
-1 7
-1 8
-1 9
-1 10
-1 -1
0
输出样例
1 1 1
512 256 128 64 32 16 8 4 2 1
提示
Note that the first test case in the sample input describes a tree looking like
  2  

/ \
1 3


Make sure the sum of your frequency won't exceed 263 - 1, as special judge may sum all frequency up.

该题目是Virtual Judge题目,来自 北京大学POJ

题目来源 Ulm Local 2005

源链接: POJ-2477

最后修改于 2020-10-29T06:33:37+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
1000 65536