Graphs
Practice
2.8 (14 votes)
Algorithms
Breadth first search
C++
Depth first search
Easy
Graphs
Problem
81% Success 2112 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

You are given an undirected graph \(G\) that contains \(n\) nodes and \(m\) edges. It is also mentioned that \(G\) does not contain any cycles. A sequence of nodes \((A_1, A_2, A_3, ... A_k)\) is special if distance \(d(A_i, A_{i+1}) = f.i\) \(\forall\) \(1 \leq i < k\), and all \(A_i\) are distinct. Determine the size of a maximal special sequence. Hence, the one with maximum \(k\).

Note 

  • If \(A\) and \(B\) are not connected, then \(d(A, B)\) \(= \infty\), else \(d(A, B) = \) the number of edges in the path from\(A\) to \(B\).
  • Any node can be selected as a sequence of size \(1\).

Input format 

  • First line: Three integers \(n\) , \(m\), and \(f\)
  • Next second to \((m+1)^{th}\) lines: Two integers \(x\) and \(y,\) \(x \neq y\), that denotes an edge between \(x\) and \(y\)

Output format

Print the maximum value of \(k\).

Constraints

\(1 \leq f,n < 100000\)\(, 1 \leq m < n\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
444 votes
Tags:
Breadth First SearchDepth First SearchDynamic ProgrammingEasyGraphsMathNumber Theory
Points:30
61 votes
Tags:
Ad-HocAlgorithmsBreadth First SearchEasyGraphs
Points:30
43 votes
Tags:
AlgorithmsBreadth First SearchEasyGraphs