Graphs

2.8

14 votes
Algorithms, Breadth First Search, C++, Depth First Search, Easy, Graphs
Problem

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 (A1,A2,A3,...Ak) is special if distance d(Ai,Ai+1)=f.i  1i<k, and all Ai 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) =, else d(A,B)= the number of edges in the path fromA 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, xy, that denotes an edge between x and y

Output format

Print the maximum value of k.

Constraints

1f,n<100000,1m<n

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

This graph is not connected. Two separate components are formed denoted as follows:

12345

In this case, the maximal beautiful sequence is of length 3

Editor Image

?