Strong Vertices
题目链接
题面翻译
给定两个数组 a a a 和 b b b,对此构造一张有向图:
- 若 a u − a v ≥ b u − b v a_u-a_v\ge b_u-b_v au−av≥bu−bv,则 u u u 向 v v v 连边。
求所有向其他所有顶点连边的顶点个数,并按从小到大顺序输出它们。
题目描述
Given two arrays a a a and b b b , both of length n n n . Elements of both arrays indexed from 1 1 1 to n n n . You are constructing a directed graph, where edge from u u u to v v v ( u ≠ v u\neq v u=v ) exists if a u − a v ≥ b u − b v a_u-a_v \ge b_u-b_v au−av≥bu−bv .
A vertex V V V is called strong if there exists a path from V V V 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 u u u , along the directions of the edges, the vertex v v v can be reached.
Your task is to find all strong vertices.
For example, if a = [ 3 , 1 , 2 , 4 ] a=[3,1,2,4] a=[3,1,2,4] and b = [ 4 , 3 , 2 , 1 ] 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
4
4
4
输入格式
The first line contains an integer t t t ( 1 ≤ t ≤ 1 0 4 1\le t\le 10^4 1≤t≤104 ) — the number of test cases.
The first line of each test case contains an integer n n n ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \le n \le 2\cdot 10^5 2≤n≤2⋅105 ) — the length of a a a and b b b .
The second line of each test case contains n n n integers a 1 , a 2 … a n a_1,a_2 \dots a_n a1,a2…an ( − 1 0 9 ≤ a i ≤ 1 0 9 -10^9 \le a_i \le 10^9 −109≤ai≤109 ) — the array a a a .
The third line of each test case contains n n n integers b 1 , b 2 … b n b_1,b_2 \dots b_n b1,b2…bn ( − 1 0 9 ≤ b i ≤ 1 0 9 -10^9 \le b_i \le 10^9 −109≤bi≤109 ) — the array b b b .
It is guaranteed that the sum of $ n $ for all test cases does not exceed 2 ⋅ 1 0 5 2\cdot 10^5 2⋅105 .
输出格式
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
样例输入 #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
3
3
3 and
5
5
5 . Note that there is a bidirectional edge between vertices
3
3
3 and
5
5
5 .In the third sample, the vertices are connected by a single directed edge from vertex
2
2
2 to vertex
1
1
1 , so the only strong vertex is
2
2
2 .
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.
做题思路
由题面所给公式 a u − a v ≥ b u − b v a_u-a_v\ge b_u-b_v au−av≥bu−bv ,可以得到 a u − b u ≥ a v − b v a_u - b_u\ge a_v - b_v au−bu≥av−bv,此时有从 u u u 到 v v v 的路径。取 c i = a i − b i c_i = a_i - b_i ci=ai−bi。而题目要求的是有到达所有点路径的那个起始点,那么就需要求出 c i c_i ci 的最大值。对于第 i i i 个点,如果有 c i = = m a x ( c i ) c_i == max(c_i) ci==max(ci),那么点 i i i 即为所求。
参考程序
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int a[N], b[N];
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> b[i];
int mx = INT_MIN;
for (int i = 1; i <= n; i++)
mx = max(mx, a[i] - b[i]);
int c = 0;
for (int i = 1; i <= n; i++)
c += (a[i] - b[i] == mx);
cout << c << "\n";
for (int i = 1; i <= n; i++)
if (a[i] - b[i] == mx)
cout << i << ' ';
cout << "\n";
}
}