CF1476C.Longest Simple Cycle
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
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 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.
你有 n 条链,其中第 i 条链包含 ci 个顶点。每条链内的顶点独立地沿链从 1 编号至 ci。换言之,第 i 条链是一个无向图,包含 ci 个顶点和 (ci−1) 条边,对每个满足 1≤j<ci 的 j,该图中存在一条连接第 j 个顶点与第 (j+1) 个顶点的边。
现在你决定按如下方式将这些链合并为一个图:
- 跳过第一条链;
- 将第 i 条链的第 1 个顶点与第 (i−1) 条链的第 ai 个顶点用一条边相连;
- 将第 i 条链的最后一个(即第 ci 个)顶点与第 (i−1) 条链的第 bi 个顶点用一条边相连。
第一个测试用例示意图。虚线表示在合并过程中添加的边。
请计算所得图中最长简单环的长度。
简单环是指首尾顶点也相连的一条路径;若沿该简单环遍历,环上的每个顶点恰好被访问一次。
输入格式
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.
第一行包含一个整数 t(1≤t≤1000)—— 测试用例的数量。
每个测试用例的第一行包含一个整数 n(2≤n≤105)—— 你拥有的链的数量。
每个测试用例的第二行包含 n 个整数 c1,c2,…,cn(2≤ci≤109)—— 对应各条链的顶点数。
每个测试用例的第三行包含 n 个整数 a1,a2,…,an(a1=−1;1≤ai≤ci−1)。
每个测试用例的第四行包含 n 个整数 b1,b2,…,bn(b1=−1;1≤bi≤ci−1)。
a1 和 b1 均等于 −1,它们在图的构建中不被使用,仅用于保持下标一致性。保证所有测试用例的 n 之和不超过 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.
在第一个测试用例中,最长的简单环如下所示:

我们无法通过第一条链来延长该环,因为此时它将不再是一个简单环——第二条链上的顶点 2 将破坏环的简单性。
输入解题思路,AI测评打分。不知道怎么写?