CF932F.Escape Through Leaf

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a tree with nn nodes (numbered from 11 to nn ) rooted at node 11 . Also, each node has two values associated with it. The values for ii -th node are aia_{i} and bib_{i} .

You can jump from a node to any node in its subtree. The cost of one jump from node xx to node yy is the product of axa_{x} and byb_{y} . The total cost of a path formed by one or more jumps is sum of costs of individual jumps. For every node, calculate the minimum total cost to reach any leaf from that node. Pay attention, that root can never be leaf, even if it has degree 11 .

Note that you cannot jump from a node to itself.

输入格式

The first line of input contains an integer nn ( 2<=n<=1052<=n<=10^{5} ) — the number of nodes in the tree.

The second line contains nn space-separated integers a1,a2,...,an(105<=ai<=105)a_{1},a_{2},...,a_{n}(-10^{5}<=a_{i}<=10^{5}) .

The third line contains nn space-separated integers b1,b2,...,bn(105<=bi<=105)b_{1},b_{2},...,b_{n}(-10^{5}<=b_{i}<=10^{5}) .

Next n1n-1 lines contains two space-separated integers uiu_{i} and viv_{i} ( 1<=ui,vi<=n1<=u_{i},v_{i}<=n ) describing edge between nodes uiu_{i} and viv_{i} in the tree.

输出格式

Output nn space-separated integers, ii -th of which denotes the minimum cost of a path from node ii to reach any leaf.

输入输出样例

  • 输入#1

    3
    2 10 -1
    7 -7 5
    2 3
    2 1
    

    输出#1

    10 50 0 
  • 输入#2

    4
    5 -10 5 7
    -8 -80 -3 -10
    2 1
    2 4
    1 3
    

    输出#2

    -300 100 0 0 

说明/提示

In the first example, node 33 is already a leaf, so the cost is 00 . For node 22 , jump to node 33 with cost a2×b3=50a_{2}×b_{3}=50 . For node 11 , jump directly to node 33 with cost a1×b3=10a_{1}×b_{3}=10 .

In the second example, node 33 and node 44 are leaves, so the cost is 00 . For node 22 , jump to node 44 with cost a2×b4=100a_{2}×b_{4}=100 . For node 11 , jump to node 22 with cost a1×b2=400a_{1}×b_{2}=-400 followed by a jump from 22 to 44 with cost a2×b4=100a_{2}×b_{4}=100 .

首页