Color a Tree
| Time Limit: 1000MS |
|
Memory Limit: 30000K |
| Total Submissions: 2095 |
|
Accepted: 564 |
Description 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.
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. Input 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-1 lines 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. Output For each test case, output a line containing the minimum total coloring cost required for Bob to color all the nodes.Sample Input 5 11 2 1 2 41 21 32 43 50 0 Sample Output 33 題目大意: 給你一個有N個節點的樹,每個節點都有一個權值。現在你要給這棵樹染色,染色的規則是: (1)根節點可以隨時給它染色 (2)若要給某個節點染色,必須已經給它的父節點染好了色 每次染色的代價是(I*VAL[J])I代表第幾次染色。求最小代價。 輸入第一行兩個整數N M代表一共有N個節點點,根接點是M。 第二行N個整數代表每個節點的權值。 以下N-1行每行兩個整數A B代表A是B的父親。 解析: 我是看報告寫的,看報告吧……這個就是所謂的噁心題。 我們這樣來思考,在染色了Pa(Max)之後,就要馬上進行Max結點染色。合并之後成為一個Union結點,他的權值是二者的和,而將Union節點染色的時間為2。同時Max和Pa(Max)的所有兒子均為Union的兒子,這樣就根據題意完成了合并Pa(max)和Max兩個結點的工作。由於合并產生了染色需要時間的關係,下面對模型進行擴充:每個結點有一個權值Ci,和一個染色所需要的時間Si,而且每個結點還有一個產生序列,表示依次是哪些結點合并而成。所以我們的貪心思想也有所變化,盡量讓Ci/Si比值最大的節點先染色。這很好理解,如果將合并了的結點拆開來看,事實上可以認為是Si個權值為Ci/Si的結點。根據合并的方式,我們得到了一個演算法: 1. 令所有節點S值均為1,每個節點產生序列中僅有一個元素,即為它本身。 2. 若樹中只剩一個結點,則輸出這個這個結點的產生序列。 3. 取出Ci/Si值最大的非根結點Max。 4. 將Max和其父親合并,新合并出的結點Union的各個參數為:CunionßCMax+CPa(max),SUnionßSMax+SPa(Max),同時Union的產生序列為Pa(Max)的產生序列與Max的產生序列串連而成。 5. 重複2~4步。 我們來分析一下整個演算法的時間複雜度,維護所有結點集合,快速找出權值最大的非根結點可以使用二元堆積。而對結點進行和並可以使用並查集。其中維護堆總時間複雜度為O(Nlog2N),而並查集時間複雜度為O(N*a(n)),所以總時間複雜度為O(Nlog2N)。
很噁心,我看完題目之後只能想到是個貪心,因為樹歸不能傳遞兩個子樹分別的權值,所以有後效性。可是怎麼都想不到是這樣貪的……
(轉載於:http://hi.baidu.com/gugugupan/blog/item/710628f56cad3268ddc474f9.html) |