The heart of Conscience ↓
http://z55250825.blog.163.com/blog/static/150230809201412793151890/
Tarjan when the tree is a tree-like DP (encounter ring ignore), and finally in the Tarjan backtracking before the current point is the "highest point" of the ring, the ring DP, this ring DP is $o (N^2) $, but if we use the Monotone queue optimization is $o (NX2) $
Total complexity $o (n) $ really infinite pitch film Otz
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int N = 500003;void Read (int &k) {k = 0; int fh = 1; char c = GetChar (); for (; c < ' 0 ' | | c > ' 9 '; c = GetChar ()) if (c = = '-') FH =- 1;for (; c >= ' 0 ' && C <= ' 9 '; c = GetChar ()) K = (k << 1) + (k << 3) + C-' 0 '; k = k * FH;} struct Node {int nxt, to;} E[n << 1];int ans = 0, point[n], N, m, cnt = 0, L, r;int Deep[n], f[n], fa[n], dfn[n], low[n], q[n << 1], tb[n << 1];void ins (int x, int y) {e[++cnt] = (node) {point[x], y}; point[x] = cnt;} void __ (int rt, int x) {int tot = deep[x]-DEEP[RT] + 1, num = tot << 1, top = tot >> 1;for (int tmp = x; tmp ! = RT; TMP = fa[tmp]) tb[tot--] = f[tmp];tb[1] = F[rt];tot = deep[x]-DEEP[RT] + 1;for (int i = 1; I <= tot; ++i) Tb[tot + i] = Tb[i];l = R = 1; Q[1] = 1;for (int i = 2; I <= num; ++i) {while (L <= r && i-q[l] > top) ++l;ans = max (ans, tb[i] + i + t B[Q[L]]-q[l]); whiLe (l <= R && Tb[q[r]]-q[r] <= Tb[i]-i)--r; Q[++r] = i;} for (int i = 2; I <= tot; ++i) F[rt] = max (F[rt], Tb[i] + min (i-1, tot-i + 1));} void _ (int x) {dfn[x] = low[x] = ++cnt;for (int tmp = point[x]; tmp = E[TMP].NXT) {int v = e[tmp].to;if (v = = fa[x]) c Ontinue;if (! Dfn[v]) {fa[v] = x; Deep[v] = deep[x] + 1;_ (v); low[x] = min (low[x], low[v]);} elselow[x] = min (low[x], dfn[v]); if (Dfn[x] < Low[v]) {ans = max (ans, f[x] + f[v] + 1); F[X] = max (f[x], F[v] + 1);}} for (int tmp = point[x]; tmp; tmp = E[TMP].NXT) {int v = e[tmp].to;if (x = = Fa[v] | | DFN[X] > Dfn[v]) continue;__ (x, v);}} int main () {read (n); Read (m), int u, V, k;for (int i = 1; I <= m; ++i) {read (k); Read (U), for (--k; k;--k) {read (v); INS (U , v); Ins (v, u); U = V;}} CNT = 0;_ (1);p rintf ("%d\n", ans); return 0;}
Cactus Although partial, but do not know also can not, this code is I knock the result of a night Qaq
"Bzoj 1023" "SHOI 2008" Cactus Cactus Diagram