A Cactus Problem

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 0.6s
Memory limit: 162M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

Given a cactus graph, compute the maximum distance between two vertices.


1 \le N \le 5 \cdot 10^4

0 \le M \le 10^4

2 \le K_i \le 10^3

Input Specification

The first line contains two space-separated integers, N and M. There are N vertices labeled from 1 to N.

M lines follow. Each line starts with an integer K_i, followed by K_i space-separated integers. Adjacent integers in each line are connected with an edge. The graph is guaranteed to be a connected cactus.

Output Specification

Output the diameter of the graph.

Sample Input

15 3
9 1 2 3 4 5 6 7 8 3
7 2 9 10 11 12 13 10
5 2 14 9 15 10

Sample Output