F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Transmigration tree

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 361    Accepted Submission(s): 81


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 \leq 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 \leq n \leq 50000, 1 \leq q < 100000)$.

The second line contains $n - 1$ integers $A_i ( 1 \leq A_i \leq i )$ represent the node $i + 1$'s father is node $A_i$.

The third line contains $n$ integers $B_i ( 1 \leq i \leq n, 0 \leq B_i \leq 1000)$ represent the node $i$'s weight is $B_i$.

Then $q$ line follow,each line contains two integers $u,v$,it represents a query$(1 \leq u,v \leq 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
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-05-07 16:41:11, Gzip enabled