Tap here to switch tabs
Problem
Ask
Submissions

Problem: Reorder Routes to Make All Paths Lead to the City Zero

med
30 min
Explore how to identify and reorder directed roads within a city network that forms a tree structure to ensure all paths lead to the capital city labeled zero. Understand the problem constraints and develop an algorithm to find the minimum number of road reassignments required, improving your graph theory and problem-solving skills.

Statement

There are n cities labeled from 00 to n1n - 1, connected by n1n - 1 roads, so there is only one route between any two cities. This road network forms a tree structure.

Last year, the Ministry of transport made all roads one-way due to their narrow width. These roads are represented as connections, where each entry connections[i] =[ai,bi]= [a_i, b_i] means there is a road going from city aia_i to city bib_i​.

This year, a major event will occur in the capital city (city 00), and people from all other cities must reach it. Your task is to reorient some roads so every city has a valid path leading to city 00. Return the minimum number of roads that must be changed to achieve this.

Note: A solution is guaranteed to exist, meaning that every city can eventually reach city 00 after reordering.

Constraints:

  • 22 \leq n 5104\leq 5 * 10^4

  • connections.length ==n1== n -1

  • connections[i].length ==2== 2

  • 0ai,bin10 \leq a_i, b_i \leq n - 1

Tap here to switch tabs
Problem
Ask
Submissions

Problem: Reorder Routes to Make All Paths Lead to the City Zero

med
30 min
Explore how to identify and reorder directed roads within a city network that forms a tree structure to ensure all paths lead to the capital city labeled zero. Understand the problem constraints and develop an algorithm to find the minimum number of road reassignments required, improving your graph theory and problem-solving skills.

Statement

There are n cities labeled from 00 to n1n - 1, connected by n1n - 1 roads, so there is only one route between any two cities. This road network forms a tree structure.

Last year, the Ministry of transport made all roads one-way due to their narrow width. These roads are represented as connections, where each entry connections[i] =[ai,bi]= [a_i, b_i] means there is a road going from city aia_i to city bib_i​.

This year, a major event will occur in the capital city (city 00), and people from all other cities must reach it. Your task is to reorient some roads so every city has a valid path leading to city 00. Return the minimum number of roads that must be changed to achieve this.

Note: A solution is guaranteed to exist, meaning that every city can eventually reach city 00 after reordering.

Constraints:

  • 22 \leq n 5104\leq 5 * 10^4

  • connections.length ==n1== n -1

  • connections[i].length ==2== 2

  • 0ai,bin10 \leq a_i, b_i \leq n - 1