编程介的小学生 2018-12-12 06:12 采纳率: 20.5%
浏览 429
已采纳

二叉树的转换问题将权重转换到根节点,怎么用C实现呢

Problem Description
There is a tree, n nodes, numbered from 1 to n, each node has a weight, but each leaf node is the father of the root,

we call such a tree as Transmigration tree, The root of each tree is node 1.

We need to minimize the sum of weights that node u to node v and know who is the biggest weights node in this path.

Input
The first line of the input is a single integer T(T≤10), indicating the number of testcases.

Then T testcases follow.In each testcase,

The first line contains two integers n,q, the number of nodes and inquiry(2≤n≤50000,1≤q<100000).

The second line contains n−1 integers Ai(1≤Ai≤i) represent the node i+1's father is node Ai.

The third line contains n integers Bi(1≤i≤n,0≤Bi≤1000) represent the node i's weight is Bi.

Then q line follow,each line contains two integers u,v,it represents a query(1≤u,v≤n).

Output
For each query,we should output one line "x y", where x is the smallest the sum of weights that node u to node v and y is the weight of the biggest weights node in this path.(If there are multiple paths, please calculating the maximum node weights largest)

Sample Input
1
5 1
1 2 3 4
413 127 263 869 960
1 5

Sample Output
1373 960

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-06-29 22:11
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于#flink#的问题:关于docker部署flink集成hadoop的yarn,请教个问题flink启动yarn-session.sh连不上hadoop
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题