CF1476C.Longest Simple Cycle

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You have nn chains, the ii -th chain consists of cic_i vertices. Vertices in each chain are numbered independently from 11 to cic_i along the chain. In other words, the ii -th chain is the undirected graph with cic_i vertices and (ci1)(c_i - 1) edges connecting the jj -th and the (j+1)(j + 1) -th vertices for each 1j<ci1 \le j < c_i .

Now you decided to unite chains in one graph in the following way:

  1. the first chain is skipped;
  2. the 11 -st vertex of the ii -th chain is connected by an edge with the aia_i -th vertex of the (i1)(i - 1) -th chain;
  3. the last ( cic_i -th) vertex of the ii -th chain is connected by an edge with the bib_i -th vertex of the (i1)(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 tt ( 1t10001 \le t \le 1000 ) — the number of test cases.

The first line of each test case contains the single integer nn ( 2n1052 \le n \le 10^5 ) — the number of chains you have.

The second line of each test case contains nn integers c1,c2,,cnc_1, c_2, \dots, c_n ( 2ci1092 \le c_i \le 10^9 ) — the number of vertices in the corresponding chains.

The third line of each test case contains nn integers a1,a2,,ana_1, a_2, \dots, a_n ( a1=1a_1 = -1 ; 1aici11 \le a_i \le c_{i - 1} ).

The fourth line of each test case contains nn integers b1,b2,,bnb_1, b_2, \dots, b_n ( b1=1b_1 = -1 ; 1bici11 \le b_i \le c_{i - 1} ).

Both a1a_1 and b1b_1 are equal to 1-1 , they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of nn over all test cases doesn't exceed 10510^5 .

输出格式

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 22 on the second chain will break simplicity.

首页