oneby7

Advanced Graphs Special edges and paths

490. Articulation points

Hard

You are given an undirected graph with n vertices, numbered from 0 to n - 1. Each pair [u, v] in edges is an edge between vertices u and v.

A vertex is an articulation point if removing it, together with its edges, splits the piece of the graph it belongs to into two or more pieces. The graph itself can already be in several pieces, with no edges between them.

Return every articulation point in increasing order, each one once. If the graph has none, return [-1].

Example 1

0123456

Input: n = 7 edges = [[0, 1], [1, 2], [2, 0], [1, 3], [3, 4], [4, 5], [5, 3], [5, 6]]

Output: [1,3,5]

Explanation: Removing 1 cuts 0 and 2 off from the rest. Removing 3 leaves 0, 1, 2 in one piece and 4, 5, 6 in another. Removing 5 leaves 6 alone. Every other vertex can go, and what remains of the graph stays joined.

Example 2

0123

Input: n = 4, edges = [[0, 1], [1, 2], [2, 3], [3, 0]]

Output: [-1]

Explanation: The four vertices form a ring. Removing any one of them leaves a path through the other three, so there is no articulation point.

Example 3

012345

Input: n = 6, edges = [[4, 2], [2, 5], [0, 1]]

Output: [2]

Explanation: The graph is in three pieces, and vertex 3 has no edges at all. Removing 2 splits its piece into 4 and 5. Removing 0, 1, 4 or 5 leaves a single vertex behind, which is still one piece.

Check your understanding

Work out the answer for this input before you write any code.

n = 6, edges = [[0, 1], [1, 2], [2, 0], [3, 4]]

Constraints

  • 1 ≤ n ≤ 105
  • 0 ≤ edges.length ≤ 105
  • edges[i].length == 2
  • 0 ≤ ui, vi < n
  • ui ≠ vi
  • No edge appears twice: [u, v] and [v, u] are the same edge.

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.