CF1476C.Longest Simple Cycle

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

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 (ci−1)(c_i - 1) edges connecting the jj-th and the (j+1)(j + 1)-th vertices for each 1≤j<ci1 \le j \lt 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 (i−1)(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 (i−1)(i - 1)-th chain.

Picture of the first test case. Dotted lines are the edges added during uniting process

Calculate 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.

你有 nn 条链,其中第 ii 条链包含 cic_i 个顶点。每条链内的顶点独立地沿链从 11 编号至 cic_i。换言之,第 ii 条链是一个无向图,包含 cic_i 个顶点和 (ci−1)(c_i - 1) 条边,对每个满足 1≤j<ci1 \le j < c_i 的 jj,该图中存在一条连接第 jj 个顶点与第 (j+1)(j + 1) 个顶点的边。

现在你决定按如下方式将这些链合并为一个图:

  1. 跳过第一条链;
  2. 将第 ii 条链的第 11 个顶点与第 (i−1)(i - 1) 条链的第 aia_i 个顶点用一条边相连;
  3. 将第 ii 条链的最后一个(即第 cic_i 个)顶点与第 (i−1)(i - 1) 条链的第 bib_i 个顶点用一条边相连。

第一个测试用例示意图。虚线表示在合并过程中添加的边。

请计算所得图中最长简单环的长度。

简单环是指首尾顶点也相连的一条路径;若沿该简单环遍历,环上的每个顶点恰好被访问一次。

输入格式

The first line contains a single integer tt (1≤t≤10001 \le t \le 1000) — the number of test cases.

The first line of each test case contains the single integer nn (2≤n≤1052 \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 (2≤ci≤1092 \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; 1≤ai≤ci−11 \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; 1≤bi≤ci−11 \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.

第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(2≤n≤1052 \le n \le 10^5)—— 你拥有的链的数量。

每个测试用例的第二行包含 nn 个整数 c1,c2,…,cnc_1, c_2, \dots, c_n(2≤ci≤1092 \le c_i \le 10^9)—— 对应各条链的顶点数。

每个测试用例的第三行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(a1=−1a_1 = -1;1≤ai≤ci−11 \le a_i \le c_{i - 1})。

每个测试用例的第四行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \dots, b_n(b1=−1b_1 = -1;1≤bi≤ci−11 \le b_i \le c_{i - 1})。

a1a_1 和 b1b_1 均等于 −1-1,它们在图的构建中不被使用,仅用于保持下标一致性。保证所有测试用例的 nn 之和不超过 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.

在第一个测试用例中,最长的简单环如下所示:

我们无法通过第一条链来延长该环,因为此时它将不再是一个简单环——第二条链上的顶点 22 将破坏环的简单性。

输入解题思路,AI测评打分。不知道怎么写?

首页