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
| #include <iostream> #include <cstdio> #include <cstring>
using namespace std;
const int N = 1e5 + 10, M = 2e7 + 10; int n, m; int head[N], nxt[M], to[M], e_tot = 1; int dfn[N], low[N], fa[N], tim; int f[2 * N], a[2 * N], cnt; int q[4 * N], h, t; int ans;
void add(int x, int y) { nxt[++e_tot] = head[x], head[x] = e_tot, to[e_tot] = y; }
void calc(int u, int v) { int x = v; cnt = 0, h = 1, t = 0; while (x != u) { a[++cnt] = x, x = fa[x]; } a[++cnt] = u; for (int i = 1; i <= cnt; ++i) a[i + cnt] = a[i]; for (int i = 1; i <= 2 * cnt; ++i) { if (h <= t && q[h] < i - cnt / 2) ++h; while (h <= t && f[a[q[t]]] - q[t] < f[a[i - 1]] - (i - 1)) --t; q[++t] = i - 1; ans = max(ans, f[a[i]] + i + f[a[q[h]]] - q[h]); } for (int i = 1; i < cnt; ++i) { f[u] = max(f[u], f[a[i]] + min(i, cnt - i)); } }
void tarjan(int u, int _fa) { dfn[u] = low[u] = ++tim; for (int i = head[u]; i; i = nxt[i]) { int v = to[i]; if (v == _fa) continue; if (!dfn[v]) { fa[v] = u; tarjan(v, u); low[u] = min(low[u], low[v]); } else low[u] = min(low[u], dfn[v]); if (low[v] > dfn[u]) { ans = max(ans, f[u] + f[v] + 1); f[u] = max(f[u], f[v] + 1); } } for (int i = head[u]; i; i = nxt[i]) { int v = to[i]; if (fa[v] != u && dfn[v] > dfn[u]) calc(u, v); } }
int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; ++i) { int k; scanf("%d", &k); for (int i = 1; i <= k; ++i) { scanf("%d", &a[i]); } for (int i = 2; i <= k; ++i) { add(a[i], a[i - 1]); add(a[i - 1], a[i]); } } tarjan(1, 0); cout << ans << endl; return 0; }
|