[WeRide] OA 2025 start – 5 Mar (generic)

Given a set of nodes and a list of connected pairs, determine the order (number of nodes) in each connected component in the graph. For each component, calculate the ceiling of the square root of its order, and return the sum of these values across all connected components.

Example

graph_nodes = 10
graph_from = [1, 1, 2, 3, 7]
graph_to = [2, 3, 4, 5, 8]

There are graph_edges = 5 edges to consider. There are 2 isolated sets with more than one node, {1, 2, 3, 4, 5} and {7, 8}. The ceilings of their square roots are 5^(1/2) ≈ 2.236 and ceil(2.236) = 3, 2^(1/2) ≈ 1.414 and ceil(1.414) = 2. The other three isolated nodes are separate and the square root of their orders is 1^(1/2) = 1 respectively. The sum is 3 + 2 + (3 * 1) = 8.

Function Description

Complete the function connectedSum in the editor below.

connectedSum has the following parameters:

int graph_nodes: the number of nodes
int graph_from[graph_edges]: an array of integers that represent one end of an edge
int graph_to[graph_edges]: an array of integers that represent the other end of an edge

Returns:
int an integer that denotes the sum of the values calculated

Constraints

2 ≤ graph_nodes ≤ 10⁵
1 ≤ graph_edges ≤ 10⁵


The city of Hackerland can be represented with an even number n houses arranged in a row. A painter must paint the houses using at most three colors. The following conditions must hold true:

  1. No two adjacent houses are the same color.
  2. The houses which are at the same distance from both ends must not be colored with the same color. For example, n = 6, then houses will be [1,2,3,4,5,6], so the houses at the same distance from both the ends will be [1,6], [2,5], [3,4].

The task is to find the number of ways to paint the houses using at most three colors such that both the above conditions hold true. Since the answer can be large, report it modulo 10⁹ + 7. Two ways are considered different if at least one house is colored differently.

Example

For n = 4, some of the possible valid arrangements are:

  • (color1, color2, color3, color2)
  • (color1, color3, color1, color3)

The number of ways to paint 4 houses using three colors is 18. Return 18 modulo (10⁹ + 7) which is 18.

Function Description

Complete the countWaysToColorHouses function in the editor below.

countWaysToColorHouses takes in a single parameter:

  • int n: the number of houses

Return

  • int: the number of ways in which the houses can be colored, calculated as a modulo of (10⁹ + 7)

Constraints

  • 2 ≤ n ≤ 100000
  • n is an even integer.

Given a number of cities positioned on a Cartesian plane, each city is located at an integer (x, y) coordinate. City names and their locations are provided in three arrays: cityName, x, and y, where cityName[i] is the name of the city and (x[i], y[i]) are its coordinates. For each queried city, find the name of the nearest city that shares either the same x or y coordinate. If no other cities share an x or y coordinate, return 'NONE'. If multiple cities are equidistant to the queried city, choose the one with the alphabetically lowest name. The distance is calculated using the Manhattan distance, which is the sum of the absolute differences in x and y coordinates.

Example

Input

n = 3
cityName = ['c1', 'c2', 'c3']
x = [3, 2, 1]
y = [3, 2, 3]
q = ['c1', 'c2', 'c3']

Explanation

The three points at (x[i], y[i]) are (3,3), (2,2), and (1,3), representing the coordinates of the cities on the graph.

  • The nearest city to c1 is c3 (distance = |3-1| + |3-3| = 2).
  • City c2 does not have a nearest city as no other cities share an x or y with c2, so this query returns 'NONE'.
  • A query of c3 returns c1 based on the first calculation.

Output

The return array after all queries are complete is ['c3', 'NONE', 'c1'].

Function Description

Complete the function closestStraightCity in the editor with the following parameters:

  • string cityName[n]: an array of strings that represent the names of each city[i]
  • int x[n]: the x coordinates of each city[i]
  • int y[n]: the y coordinates of each city[i]
  • string q[m]: the names of each city to query

Returns

  • string[m]: an array of m strings where the index of the iᵗʰ element denotes the return value of the index of the iᵗʰ query

我们长期稳定承接各大科技公司如TikTok、Google、Amazon等的OA笔试代写服务,确保满分通过。如有需求,请随时联系我们。

We consistently provide professional online assessment services for major tech companies like TikTok, Google, and Amazon, guaranteeing perfect scores. Feel free to contact us if you're interested.

Leave a Reply

Your email address will not be published. Required fields are marked *