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
| #include <cstdio> #include <list> #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 = 5e4 + 10; int n, m, p[N], f[N][2]; vector <int> g[N]; int stmp, dfn[N], low[N]; int calc (list <int> &h) { int k[2] = { 0, 0 }; for (int i : h) { int tk[2] = { k[0], k[1] }; k[0] = max(tk[0], tk[1]) + f[i][0]; k[1] = tk[0] + f[i][1]; } return max(k[0], k[1]); } void tarjan (int u) { dfn[u] = low[u] = ++stmp; f[u][0] = 0, f[u][1] = 1; 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]) { f[u][0] += max(f[v][0], f[v][1]); f[u][1] += f[v][0]; } } else low[u] = min(low[u], dfn[v]); for (int v : g[u]) if (dfn[v] > dfn[u] && p[v] ^ u) { list <int> h; for (int t = v; t ^ u; t = p[t]) h.pb(t); f[u][0] += calc(h); f[u][1] += f[h.front()][0] + f[h.back()][0]; h.pop_front(), h.pop_back(); f[u][1] += calc(h); } } int main () { read(n, m); for (int a, b; m; --m) { read(a, b); g[a].pb(b), g[b].pb(a); } tarjan(1); write(max(f[1][0], f[1][1])); return 0; }
|