MTREECOL - Color a tree

题意翻译

Bob对一棵树的数据结构非常感兴趣。树是一个有向图,其中一个特殊的节点被挑选出来,被称为树的“根”,并且从根到每个其他节点都有唯一的路径。 Bob打算用笔来着色树的所有节点。一棵树有n个节点,这些节点编号为1, 2,…,n。假设一个节点着色需要1个单位时间,并且在完成一个节点着色之后,允许他对另一个节点着色。另外,只有当父节点被着色时,才允许他对节点着色。显然,Bob只允许在第一次尝试中着色根。 每个节点都有一个“着色成本因子”,Ci。每个节点的着色成本取决于Ci和Bob完成该节点着色的时间。开始时,时间设置为0。如果着色节点i的完成时间是FI,则节点I的着色代价是Ci*Fi。 例如,在图1中示出了一个具有五个节点的树。每个节点的着色成本因子分别为1,2,1,2和4。Bob可以以1,3,5,2,4的顺序对树进行着色,最小总着色成本为33。 ![](https://cdn.luogu.org/upload/vjudge_pic/SP3912/2e4cd5d4d4a8e87535662db542c473518148a4fb.png) 给定一个树和每个节点的着色代价因子,请帮助Bob找到着色所有节点的最小可能的总着色代价。 输入输出格式 输入格式: 输入由几个测试数据组成。每一行的第一行包含两个整数n和r(1<n<=1000, 1 <=r<=n),其中n是树中的节点数,r是根节点的节点数。第二行包含n个整数,其中第i个是CI(1<CI=500),节点i的着色代价因子I。每个下一个N-1行包含两个空间分离的节点编号V1和V2,它们是树中的边的端点,表示V1是V2的父节点。 没有边将被列出两次,所有的边将被列出。 n=0和r=0的测试样例表示输入的结束,不应该被处理。 样例输入 5 1 1 2 1 1 2 4 1 2 1 3 2 4 3 5 0 0 输出格式: 对于每个测试样例,输出一个包含Bob所需的最小总着色代价的行,以对所有节点着色。 样本输出 33

题目描述

[English](/problems/MTREECOL/en/) [Vietnamese](/problems/MTREECOL/vn/)Bob is very interested in the data structure of a tree. A tree is a directed graph in which a special node is singled out, called the "root" of the tree, and there is a unique path from the root to each of the other nodes. Bob intends to color all the nodes of a tree with a pen. A tree has N nodes, these nodes are numbered 1, 2, ..., N. Suppose coloring a node takes 1 unit of time, and after finishing coloring one node, he is allowed to color another. Additionally, he is allowed to color a node only when its father node has been colored. Obviously, Bob is only allowed to color the root in the first try. Each node has a “coloring cost factor”, Ci. The coloring cost of each node depends both on Ci and the time at which Bob finishes the coloring of this node. At the beginning, the time is set to 0. If the finishing time of coloring node i is Fi, then the coloring cost of node i is Ci \* Fi. For example, a tree with five nodes is shown in Figure-1. The coloring cost factors of each node are 1, 2, 1, 2 and 4. Bob can color the tree in the order 1, 3, 5, 2, 4, with the minimum total coloring cost of 33. [](http://tinypic.com) ![Image and video hosting by TinyPic](https://cdn.luogu.com.cn/upload/vjudge_pic/SP3912/2e4cd5d4d4a8e87535662db542c473518148a4fb.png) Given a tree and the coloring cost factor of each node, please help Bob to find the minimum possible total coloring cost for coloring all the nodes.

输入输出格式

输入格式


The input consists of several test cases. The first line of each case contains two integers N and R (1 <= N <= 1000, 1 <= R <= N), where N is the number of nodes in the tree and R is the node number of the root node. The second line contains N integers, the i-th of which is Ci (1 <= Ci <= 500), the coloring cost factor of node i. Each of the next N-1lines contains two space-separated node numbers V1 and V2, which are the endpoints of an edge in the tree, denoting that V1 is the father node of V2. No edge will be listed twice, and all edges will be listed. A test case of N = 0 and R = 0 indicates the end of input, and should not be processed. ``` Sample Input 5 1 1 2 1 2 4 1 2 1 3 2 4 3 5 0 0 ```

输出格式


For each test case, output a line containing the minimum total coloring cost required for Bob to color all the nodes ``` Sample output 33 ```

输入输出样例

暂无测试点