RT。WA on #6。讨论区解法只会变成 70pts。
#include <bits/stdc++.h>
using namespace std;
namespace liuzimingc {
const int N = 1e5 + 5;
int n, m, dfn[N], low[N], cnt, bcc_cnt, tot, ans;
int f[N], g[N];
vector<int> e[N];
vector<int> bcc_e[N];
stack<int> s;
int a[N], fa[N], q[N];
bool vis[N];
void dfs(int u, int fa) {
dfn[u] = low[u] = ++cnt;
s.push(u);
for (const int &v : e[u]) {
if (v == fa) continue;
if (!dfn[v]) {
dfs(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
if (s.top() == v) {
s.pop();
bcc_e[u].push_back(v);
bcc_e[v].push_back(u);
continue;
}
bcc_cnt++;
int t;
do {
t = s.top(); s.pop();
bcc_e[bcc_cnt].push_back(t);
} while (t != v);
bcc_e[u].push_back(bcc_cnt);
}
}
else low[u] = min(low[u], dfn[v]);
}
}
void dfs2(int u, int fa) {
if (u <= n) {
for (const int &v : bcc_e[u]) {
if (v == fa) continue;
dfs2(v, u);
}
for (const int &v : bcc_e[u]) {
if (v == fa) continue;
if (v <= n) {
ans = max(ans, f[u] + f[v] + 1);
f[u] = max(f[u], f[v] + 1);
}
else {
ans = max(ans, f[u] + f[v]);
f[u] = max(f[u], f[v]);
}
}
}
else {
for (const int &v : bcc_e[u]) dfs2(v, u);
int tot = 0;
for (const int &v : bcc_e[u]) g[++tot] = f[v];
g[++tot] = f[u];
for (int i = 1; i <= tot; i++) g[i + tot] = g[i];
int head = 1, tail = 0;
q[++tail] = 0;
for (int i = 1; i <= tot * 2; i++) {
while (head < tail && i - q[head] > tot / 2) head++;
ans = max(ans, i + g[i] + g[q[head]] - q[head]);
while (head < tail && g[i] - i >= g[q[tail]] - q[tail]) tail--;
q[++tail] = i;
}
for (int i = 1; i <= tot; i++) f[u] = max(f[u], g[i] + min(i, tot - i));
}
}
int main() {
cin >> n >> m;
while (m--) {
int k;
cin >> k;
for (int i = 1; i <= k; i++) {
cin >> a[i];
if (i > 1) e[a[i]].push_back(a[i - 1]), e[a[i - 1]].push_back(a[i]);
}
}
bcc_cnt = n;
dfs(1, 0);
dfs2(1, 0);
cout << ans << endl;
return 0;
}
#undef int
}
int main() {
liuzimingc::main();
return 0;
}