CF1857D.Strong Vertices

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Given two arrays aa and bb, both of length nn. Elements of both arrays indexed from 11 to nn. You are constructing a directed graph, where edge from uu to vv (u≠vu\neq v) exists if au−av≥bu−bva_u-a_v \ge b_u-b_v.

A vertex VV is called strong if there exists a path from VV to all other vertices.

A path in a directed graph is a chain of several vertices, connected by edges, such that moving from the vertex uu, along the directions of the edges, the vertex vv can be reached.

Your task is to find all strong vertices.

For example, if a=[3,1,2,4]a=[3,1,2,4] and b=[4,3,2,1]b=[4,3,2,1], the graph will look like this:

The graph has only one strong vertex with number 44

给定两个长度均为 nn 的数组 aa 和 bb,两个数组的元素下标均从 11 到 nn。你需要构造一个有向图,其中存在一条从顶点 uu 指向顶点 vv(u≠vu \neq v)的有向边,当且仅当满足不等式 au−av≥bu−bva_u - a_v \ge b_u - b_v。

若存在一条从顶点 VV 出发、可到达图中所有其他顶点的有向路径,则称顶点 VV 为强顶点。

有向图中的一条路径是指若干顶点构成的序列,相邻顶点之间由有向边连接,且沿边的方向可以从起始顶点 uu 到达终点顶点 vv。

你的任务是找出所有的强顶点。

例如,当 a=[3,1,2,4]a = [3,1,2,4] 且 b=[4,3,2,1]b = [4,3,2,1] 时,所构造的图如下所示:


该图中唯一的强顶点编号为 44。

输入格式

The first line contains an integer tt (1≤t≤1041\le t\le 10^4) — the number of test cases.

The first line of each test case contains an integer nn (2≤n≤2⋅1052 \le n \le 2\cdot 10^5) — the length of aa and bb.

The second line of each test case contains nn integers a1,a2…ana_1,a_2 \dots a_n (−109≤ai≤109-10^9 \le a_i \le 10^9) — the array aa.

The third line of each test case contains nn integers b1,b2…bnb_1,b_2 \dots b_n (−109≤bi≤109-10^9 \le b_i \le 10^9) — the array bb.

It is guaranteed that the sum of nn for all test cases does not exceed 2⋅1052\cdot 10^5.

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

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2\cdot 10^5)—— 数组 aa 和 bb 的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2…ana_1,a_2 \dots a_n(−109≤ai≤109-10^9 \le a_i \le 10^9)—— 数组 aa。

每个测试用例的第三行包含 nn 个整数 b1,b2…bnb_1,b_2 \dots b_n(−109≤bi≤109-10^9 \le b_i \le 10^9)—— 数组 bb。

保证所有测试用例的 nn 之和不超过 2⋅1052\cdot 10^5。

输出格式

For each test case, output two lines: in the first line, output the number of strong vertices, and in the second line, output all strong vertices in ascending order.

对于每个测试用例,输出两行:第一行输出强顶点的数量,第二行按升序输出所有强顶点。

输入输出样例

  • 输入#1

    5
    4
    3 1 2 4
    4 3 2 1
    5
    1 2 4 1 2
    5 2 3 3 1
    2
    1 2
    2 1
    3
    0 2 1
    1 3 2
    3
    5 7 4
    -2 -3 -6

    输出#1

    1
    4 
    2
    3 5 
    1
    2 
    3
    1 2 3 
    2
    2 3

说明/提示

The first sample is covered in the problem statement.

For the second sample, the graph looks like this:

The graph has two strong vertices with numbers 33 and 55. Note that there is a bidirectional edge between vertices 33 and 55.

In the third sample, the vertices are connected by a single directed edge from vertex 22 to vertex 11, so the only strong vertex is 22.

In the fourth sample, all vertices are connected to each other by bidirectional edges, so there is a path from every vertex to any other vertex.

第一个样例已在题目描述中给出。

对于第二个样例,图的结构如下:


该图有两个强顶点,编号分别为 33 和 55。注意,顶点 33 与顶点 55 之间存在一条无向边。

在第三个样例中,顶点之间仅由一条从顶点 22 指向顶点 11 的有向边连接,因此唯一的强顶点是 22。

在第四个样例中,所有顶点两两之间均通过无向边相连,因此任意顶点到其余任一顶点均存在路径。

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

首页