【Codeforces】Round 891 (Div. 3) D Strong Vertices 题解

Strong Vertices

题目链接

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 auavbubv,则 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 auavbubv .

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 1t104 ) — 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 2n2105 ) — 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,a2an ( − 1 0 9 ≤ a i ≤ 1 0 9 -10^9 \le a_i \le 10^9 109ai109 ) — 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,b2bn ( − 1 0 9 ≤ b i ≤ 1 0 9 -10^9 \le b_i \le 10^9 109bi109 ) — 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 2105 .

输出格式

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 auavbubv ,可以得到 a u − b u ≥ a v − b v a_u - b_u\ge a_v - b_v aubuavbv,此时有从 u u u v v v 的路径。取 c i = a i − b i c_i = a_i - b_i ci=aibi。而题目要求的是有到达所有点路径的那个起始点,那么就需要求出 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";
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值