package main import ( "bufio" "fmt" "os" "strconv" ) var sc = bufio.NewScanner(os.Stdin) func main() { sc.Buffer(make([]byte, 1000), 500000) sc.Split(bufio.ScanWords) n, m := nextInt(), nextInt() a, b := make([]int, m), make([]int, m) for i := range a { a[i], b[i] = nextInt()-1, nextInt()-1 } uf := initUnionFindTree(n) cost := make([]int, n) for i := m - 1; i >= 0; i-- { tmp := Max(cost[uf.getRoot(a[i])], cost[uf.getRoot(b[i])]) uf.uniteTree(a[i], b[i]) cost[uf.getRoot(a[i])] = tmp + 1 } ans := 0 for i := range cost { ans = Max(ans, cost[i]) } fmt.Println(ans) } func Max(x, y int) int { if x > y { return x } return y } type UnionFind struct { root []int rank []int size []int } func initUnionFindTree(n int) *UnionFind { uf := new(UnionFind) uf.root, uf.rank, uf.size = make([]int, n), make([]int, n), make([]int, n) for i := 0; i < n; i++ { uf.root[i] = -1 uf.rank[i] = 0 uf.size[i] = 1 } return uf } func (uf UnionFind) getRoot(x int) int { if uf.root[x] == -1 { return x } uf.root[x] = uf.getRoot(uf.root[x]) return uf.root[x] } func (uf UnionFind) uniteTree(x, y int) { rootx, rooty := uf.getRoot(x), uf.getRoot(y) if rootx != rooty { if uf.rank[rootx] > uf.rank[rooty] { rooty, rootx = rootx, rooty } uf.root[rootx] = rooty if uf.rank[rootx] == uf.rank[rooty] { uf.rank[rooty]++ } uf.size[rooty] += uf.size[rootx] } } func (uf UnionFind) getSize(x int) int { rootx := uf.getRoot(x) return uf.size[rootx] } func (uf UnionFind) isSame(x, y int) bool { if uf.getRoot(x) == uf.getRoot(y) { return true } return false } func nextInt() int { sc.Scan() i, e := strconv.Atoi(sc.Text()) if e != nil { panic(e) } return i } func nextString() string { sc.Scan() return sc.Text() }