cswork / OA打开练习工作台

OracleOA #4

Create Lexicographically Largest Permutation

估计简单评测数据准备中,可先阅读题面

考点(AI 标注)并查集原地数组

Given an undirected connected graph of g_nodes and M connections, traverse all nodes at least once, and store the order of traversal in A. Now create the lexicographically largest array B which is a permutation of A. Function Description Complete the function createLexicographicallyLargestPermutation in the editor. createLexicographicallyLargestPermutation has the following parameters:

  • int g_from[]: an array of integers representing the starting nodes
  • int g_to[]: an array of integers representing the ending nodes Returns int[]: the lexicographically largest permutation array B

Example 1

Input:

g_from = [4, 5, 1, 4, 3]
g_to = [5, 1, 4, 3, 2]

Output:

[5, 4, 3, 2, 1]

Explanation: This and the following 2 explanations are not from the official, so be careful when reading them :) The order of traversal A is [5, 4, 3, 2, 3, 4, 1]. The lexicographically largest permutation B is [5, 4, 3, 2, 1].

Example 2

Input:

g_from = [3, 3]
g_to = [1, 2]

Output:

[3, 2, 1]

Explanation: The order of traversal A can be [3, 1, 3, 2] or any other traversal that visits all nodes at least once. The lexicographically largest permutation B is [3, 2, 1].

Example 3

Input:

g_from = [1, 2, 3, 2, 1]
g_to = [2, 3, 4, 4, 4]

Output:

[4, 3, 2, 1]

Explanation: The order of traversal A can be [1, 2, 3, 4, 2, 1, 4, 4] or any other traversal that visits all nodes at least once. The lexicographically largest permutation B is [4, 3, 2, 1].