benchmarks.wiki / Public workspace

Humanity's Last Code Exam / 2019_E / Dead-End Detector

Problem

Answer published by the source. Consult the official source to check your work against its answer.

question title

Dead-End Detector

question content

## Problem Statement The council of your hometown has decided to improve road sign placement, especially for dead ends. They have given you a road map, and you must determine where to put up signs to mark the dead ends. They want you to use as few signs as possible. The road map is a collection of locations connected by two-way streets. The following rule describes how to obtain a complete placement of dead-end signs. Consider a street SS connecting a location xx with another location. The xx-entrance of SS gets a dead-end sign if, after entering SS from xx, it is not possible to come back to xx without making a U-turn. A U-turn is a 180-degree turn immediately reversing the direction. To save costs, you have decided not to install redundant dead-end signs, as specified by the following rule. Consider a street SS with a dead-end sign at its xx-entrance and another street TT with a dead-end sign at its yy-entrance. If, after entering SS from xx, it is possible to go to yy and enter TT without making a U-turn, the dead-end sign at the yy-entrance of TT is redundant. See Figure E.1 for examples. ## Input The first line of input contains two integers nn and mm, where nn (1n5×1051 \leq n \leq 5 \times 10^5) is the number of locations and mm (0m5×1050 \leq m \leq 5 \times 10^5) is the number of streets. Each of the following mm lines contains two integers vv and ww (1v<wn1 \leq v < w \leq n) indicating that there is a two-way street connecting locations vv and ww. All location pairs in the input are distinct. ## Output On the first line, output kk, the number of dead-end signs installed. On each of the next kk lines, output two integers vv and ww marking that a dead-end sign should be installed at the vv-entrance of a street connecting locations vv and ww. The lines describing dead-end signs must be sorted in ascending order of vv-locations, breaking ties in ascending order of ww-locations. ## Sample Input 1
Plain-text mathematical notation (without MathML)
## Problem Statement

The council of your hometown has decided to improve road sign placement, especially for dead ends. They have given you a road map, and you must determine where to put up signs to mark the dead ends. They want you to use as few signs as possible.

The road map is a collection of locations connected by two-way streets. The following rule describes how to obtain a complete placement of dead-end signs. Consider a street S connecting a location x with another location. The x-entrance of S gets a dead-end sign if, after entering S from x, it is not possible to come back to x without making a U-turn. A U-turn is a 180-degree turn immediately reversing the direction.

To save costs, you have decided not to install redundant dead-end signs, as specified by the following rule. Consider a street S with a dead-end sign at its x-entrance and another street T with a dead-end sign at its y-entrance. If, after entering S from x, it is possible to go to y and enter T without making a U-turn, the dead-end sign at the y-entrance of T is redundant. See Figure E.1 for examples.

## Input

The first line of input contains two integers n and m, where n (1≤n≤5×10⁵) is the number of locations and m (0≤m≤5×10⁵) is the number of streets. Each of the following m lines contains two integers v and w (1≤v<w≤n) indicating that there is a two-way street connecting locations v and w. All location pairs in the input are distinct.

## Output

On the first line, output k, the number of dead-end signs installed. On each of the next k lines, output two integers v and w marking that a dead-end sign should be installed at the v-entrance of a street connecting locations v and w. The lines describing dead-end signs must be sorted in ascending order of v-locations, breaking ties in ascending order of w-locations.

## Sample Input 1

Original LaTeX notation
## Problem Statement

The council of your hometown has decided to improve road sign placement, especially for dead ends. They have given you a road map, and you must determine where to put up signs to mark the dead ends. They want you to use as few signs as possible.

The road map is a collection of locations connected by two-way streets. The following rule describes how to obtain a complete placement of dead-end signs. Consider a street \(S\) connecting a location \(x\) with another location. The \(x\)-entrance of \(S\) gets a dead-end sign if, after entering \(S\) from \(x\), it is not possible to come back to \(x\) without making a U-turn. A U-turn is a 180-degree turn immediately reversing the direction.

To save costs, you have decided not to install redundant dead-end signs, as specified by the following rule. Consider a street \(S\) with a dead-end sign at its \(x\)-entrance and another street \(T\) with a dead-end sign at its \(y\)-entrance. If, after entering \(S\) from \(x\), it is possible to go to \(y\) and enter \(T\) without making a U-turn, the dead-end sign at the \(y\)-entrance of \(T\) is redundant. See Figure E.1 for examples.

## Input

The first line of input contains two integers \(n\) and \(m\), where \(n\) (\(1 \leq n \leq 5 \times 10^5\)) is the number of locations and \(m\) (\(0 \leq m \leq 5 \times 10^5\)) is the number of streets. Each of the following \(m\) lines contains two integers \(v\) and \(w\) (\(1 \leq v < w \leq n\)) indicating that there is a two-way street connecting locations \(v\) and \(w\). All location pairs in the input are distinct.

## Output

On the first line, output \(k\), the number of dead-end signs installed. On each of the next \(k\) lines, output two integers \(v\) and \(w\) marking that a dead-end sign should be installed at the \(v\)-entrance of a street connecting locations \(v\) and \(w\). The lines describing dead-end signs must be sorted in ascending order of \(v\)-locations, breaking ties in ascending order of \(w\)-locations.

## Sample Input 1

Code

6 5
1 2
1 3
2 3
4 5
5 6

## Sample Output 1

Code

2
4 5
6 5

## Sample Input 2

Code

8 8
1 2
1 3
2 3
3 4
1 5
1 6
6 7
6 8

## Sample Output 2

Code

3
1 5
1 6
3 4

platform

atcoder

Discussion

Discussion

No discussion posts on this page yet. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

See answer Answer published by the source

Artifacts

Code, notes and reproducible work shared by participants. Files are served from a separate origin.

No artifacts on this page yet. Share reproducible code or notes in a contribution. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

Source and history

Official source

initial import