问题 E: Equivalence

内存限制:512 MB 时间限制:3 S
题面:传统 评测方式:文本比较 上传者:
提交:1 通过:1

题目描述

You are given two trees �1,�2T1,T2, both with n vertices. The lengths of edges of �1T1 are given. The length of each edge is non-negative.

A tree T with n vertices is good, if there is a way to assign each edge on �2T2 with a length which satisfies the following condition:

  • For each pair �,�i,j satisfying 1≤�<�≤�1i<jn, the distances of i and j on T and �2T2 are the same.

You can perform the following operation on �1T1: select an edge on �1T1 and replace its length with any non-negative integer.

Find the minimum number of operations to make �1T1 good.

输入格式

The first line of input contains a single integer T (1≤�≤86001T8600), denoting the number of test cases.

For each test case, the first line contains one integer n (2≤�≤1062n106).

The second line contains �−1n1 integers �2,�3,⋯,��p2,p3,,pn (1≤��≤�1pin).

The third line contains �−1n1 integers ���2,���3,⋯,����val2,val3,,valn (0≤����≤1090vali109).

These two lines denotes �−1n1 edges (�,��)(u,pu) with length ����valu on �1T1.

The fourth line contains �−1n1 integers �2′,�3′,⋯,��′p2,p3,,pn (1≤��′≤�1pin), denoting �−1n1 edges (�,��′)(u,pu) on �2T2.

It is guaranteed that ∑�≤1.1⋅106n1.1106.

输出格式

For each test case, the only line contains one integer denoting the answer.

输入样例 复制

1
5
1 5 2 2 
0 2 3 1 
5 5 5 1 

输出样例 复制

1