CF1476C.Longest Simple Cycle
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You have n chains, the i -th chain consists of ci vertices. Vertices in each chain are numbered independently from 1 to ci along the chain. In other words, the i -th chain is the undirected graph with ci vertices and (ci−1) edges connecting the j -th and the (j+1) -th vertices for each 1≤j<ci .
Now you decided to unite chains in one graph in the following way:
- the first chain is skipped;
- the 1 -st vertex of the i -th chain is connected by an edge with the ai -th vertex of the (i−1) -th chain;
- the last ( ci -th) vertex of the i -th chain is connected by an edge with the bi -th vertex of the (i−1) -th chain.
Picture of the first test case. Dotted lines are the edges added during uniting processCalculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
输入格式
The first line contains a single integer t ( 1≤t≤1000 ) — the number of test cases.
The first line of each test case contains the single integer n ( 2≤n≤105 ) — the number of chains you have.
The second line of each test case contains n integers c1,c2,…,cn ( 2≤ci≤109 ) — the number of vertices in the corresponding chains.
The third line of each test case contains n integers a1,a2,…,an ( a1=−1 ; 1≤ai≤ci−1 ).
The fourth line of each test case contains n integers b1,b2,…,bn ( b1=−1 ; 1≤bi≤ci−1 ).
Both a1 and b1 are equal to −1 , they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of n over all test cases doesn't exceed 105 .
输出格式
For each test case, print the length of the longest simple cycle.
输入输出样例
输入#1
3 4 3 4 3 3 -1 1 2 2 -1 2 2 3 2 5 6 -1 5 -1 1 3 3 5 2 -1 1 1 -1 3 5
输出#1
7 11 8
说明/提示
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple — the vertex 2 on the second chain will break simplicity.