Tap here to switch tabs
Problem
Submissions

Problem: Distance to a Cycle in Undirected Graph

med
30 min
Try to solve the Distance to a Cycle in Undirected Graph problem.

Statement

You are given an integer n representing the number of vertices in a connected undirected graph, labeled from 00 to n −1- 1. You are also given a 22D integer array edges where edges[i] = [node1_i, node2_i] indicates a bidirectional edge between vertex node1_i and vertex node2_i.

The graph contains exactly one cycle.

Return an integer array answer of size n, where answer[i] represents the minimum distance from vertex i to any vertex that belongs to the cycle.

Constraints:

  • 3≤3 \leq n ≤105\leq 10^5

  • edges.length =n= n

  • edges[i].length =2= 2

  • 0≤0 \leq node1_i, node2_i ≤\leq n −1- 1

  • node1_i ≠\neq node2_i

  • The graph is connected.

  • The graph has exactly one cycle.

  • There is at most one edge between any pair of vertices.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Distance to a Cycle in Undirected Graph

med
30 min
Try to solve the Distance to a Cycle in Undirected Graph problem.

Statement

You are given an integer n representing the number of vertices in a connected undirected graph, labeled from 00 to n −1- 1. You are also given a 22D integer array edges where edges[i] = [node1_i, node2_i] indicates a bidirectional edge between vertex node1_i and vertex node2_i.

The graph contains exactly one cycle.

Return an integer array answer of size n, where answer[i] represents the minimum distance from vertex i to any vertex that belongs to the cycle.

Constraints:

  • 3≤3 \leq n ≤105\leq 10^5

  • edges.length =n= n

  • edges[i].length =2= 2

  • 0≤0 \leq node1_i, node2_i ≤\leq n −1- 1

  • node1_i ≠\neq node2_i

  • The graph is connected.

  • The graph has exactly one cycle.

  • There is at most one edge between any pair of vertices.