CF BUDDY
← Problems·

1857D · Strong Vertices

1300 · math, sortings, trees

Problem: 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 (uvu\neq v) exists if auavbubva_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

Input Format: The first line contains an integer tt (1t1041\le t\le 10^4) — the number of test cases.

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

The second line of each test case contains nn integers a1,a2ana_1,a_2 \dots a_n (109ai109-10^9 \le a_i \le 10^9) — the array aa.

The third line of each test case contains nn integers b1,b2bnb_1,b_2 \dots b_n (109bi109-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 21052\cdot 10^5.

Output Format: 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.

Note: 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.

Sample Cases

Case 1

Input

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

Output

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

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback