Problem 3045 --Acesrc and Travel

3045: Acesrc and Travel

"
Time Limit $2$ 秒/Second(s) Memory Limit $512$ 兆字节/Megabyte(s)
提交总数 $1$ 正确数量 $1$
裁判形式 标准裁判/Standard Judge 我的状态 尚未尝试
难度 分类标签
Acesrc is a famous tourist at Nanjing University second to none. During this summer holiday, he, along with Zhang and Liu, is going to travel to Hong Kong. There are nn spots in Hong Kong, and n−1n−1bidirectional sightseeing bus routes connecting these spots. They decide to visit some spots by bus. 

However, Zhang and Liu have different preferences for these spots. They respectively set a satisfactory value for each spot. If they visit the iith spot, Zhang will obtain satisfactory value aiai, and Liu will obtain bibi. Starting with Zhang, they alternately decide the next spot to visit for the sake of fairness. There must be a bus route between the current spot and the next spot to visit. Moreover, they would never like to visit a spot twice. If anyone can't find such a next spot to visit, they have no choice but to end this travel. 

Zhang and Liu are both super smart competitive programmers. Either want to maximize the difference between his total satisfactory value and the other's. Now Acesrc wonders, if they both choose optimally, what is the difference between total satisfactory values of Zhang and Liu?
The first line of input consists of a single integer TT (1≤T≤30)(1≤T≤30), denoting the number of test cases. 

For each test case, the first line contains a single integer nn (1≤n≤105)(1≤n≤105), denoting the number of spots. Each of the next two lines contains nn integers, a1,a2,⋯,ana1,a2,⋯,an and b1,b2,⋯,bnb1,b2,⋯,bn (0≤ai,bi≤109)(0≤ai,bi≤109), denoting the 
satisfactory value of Zhang and Liu for every spot, respectively. Each of the last n−1n−1 lines contains two integers x,yx,y (1≤x,y≤n,x≠y)(1≤x,y≤n,x≠y), denoting a bus route between the xxth spot and the yyth spot. It is reachable from any spot to any other spot through these bus routes. 

It is guaranteed that the sum of nn does not exceed 5.01×1055.01×105.
For each test case, print a single integer in one line, the difference of total satisfactory values if they both choose optimally.
1
3
1 1 1
0 2 3
1 2
1 3
-1

推荐代码 查看3045 所有题解 上传题解视频得图灵币

本题记录 用 户(点击查看用户) 运行号(点击购买题解) 时 间
算法最快[$548 $ms] 淡意的温柔 591132 2020-06-06 08:33:16
内存最少[$22348 $KB] 淡意的温柔 591132 2020-06-06 08:33:16
第一AC 淡意的温柔 591132 2020-06-06 08:33:16
第一挑战 淡意的温柔 591132 2020-06-06 08:33:16

赛题来源/所属竞赛 2019 Multi-University Training Contest 8 N/A

竞赛编号 竞赛名称 竞赛时间 访问比赛