1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| #include <cstdio> #include <vector> #define pb push_back using namespace std; template <class Type> void read (Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0'; flag && (x = ~x + 1); } template <class Type, class ...rest> void read (Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write (Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar('0' + x % 10); } const int N = 1e5 + 10; int hd, tl, q[N]; int n, m, ans, p[N], f[N]; vector <int> g[N]; int stmp, dfn[N], low[N]; void solve (vector <int> &h) { int l = h.size(), r = l >> 1; h.resize(l * 2); for (int i = 0; i < l; ++i) h[i + l] = h[i]; auto add = [&](int i) { while (hd <= tl && h[q[tl]] + q[tl] <= h[i] + i) --tl; q[++tl] = i; }; hd = 1, tl = 0; for (int i = 0; i < r; ++i) add(i); for (int i = 0; i < l * 2; ++i) { if (hd <= tl && q[hd] <= i) ++hd; if (i + r < l * 2) add(i + r); if (hd <= tl) ans = max(ans, h[i] + h[q[hd]] + q[hd] - i); } } void tarjan (int u) { dfn[u] = low[u] = ++stmp; for (int v : g[u]) if (v ^ p[u]) if (!dfn[v]) { p[v] = u; tarjan(v); low[u] = min(low[u], low[v]); if (low[v] > dfn[u]) { ans = max(ans, f[u] + f[v] + 1); f[u] = max(f[u], f[v] + 1); } } else low[u] = min(low[u], dfn[v]); for (int v : g[u]) if (dfn[v] > dfn[u] && p[v] ^ u) { vector <int> h; h.pb(0); for (int t = v; t ^ u; t = p[t]) h.pb(f[t]); for (int i = 1; i < h.size(); ++i) ans = max(ans, f[u] + h[i] + min(i, (int)h.size() - i)); for (int i = 1; i < h.size(); ++i) f[u] = max(f[u], h[i] + min(i, (int)h.size() - i)); solve(h); } } int main () { read(n, m); for (int k; m; --m) { read(k); for (int a, b = 0; k; --k) { read(a); if (b) g[a].pb(b), g[b].pb(a); b = a; } } tarjan(1); write(ans); return 0; }
|