/* -*- coding: utf-8 -*- * * 2072.cc: No.2072 Anatomy - yukicoder */ #include #include #include #include using namespace std; /* constant */ const int MAX_N = 200000; const int MAX_M = 200000; /* typedef */ typedef pair pii; struct UFT { vector links, ranks, sizes; UFT() {} void init(int n) { links.resize(n); for (int i = 0; i < n; i++) links[i] = i; ranks.assign(n, 1); sizes.assign(n, 1); } int root(int i) { int i0 = i; while (links[i0] != i0) i0 = links[i0]; return (links[i] = i0); } int rank(int i) { return ranks[root(i)]; } int size(int i) { return sizes[root(i)]; } bool same(int i, int j) { return root(i) == root(j); } int merge(int i0, int i1) { int r0 = root(i0), r1 = root(i1), mr; if (r0 == r1) return r0; if (ranks[r0] == ranks[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; ranks[r0]++; mr = r0; } else if (ranks[r0] > ranks[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; mr = r0; } else { links[r0] = r1; sizes[r1] += sizes[r0]; mr = r1; } return mr; } }; /* global variables */ pii es[MAX_M]; UFT uft; int ds[MAX_N]; /* subroutines */ /* main */ int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); u--, v--; es[i] = pii(u, v); } uft.init(n); for (int i = m - 1; i >= 0; i--) { int u = uft.root(es[i].first), v = uft.root(es[i].second); if (u != v) { int w = uft.merge(u, v); ds[w] = max(ds[u], ds[v]) + 1; } else ds[u]++; } printf("%d\n", ds[uft.root(0)]); return 0; }