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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| #pragma comment(linker, "/STACK:102400000,102400000") #include <stdio.h> #include <vector> #include <algorithm> #include <string.h> #include <queue> #include <stack> using namespace std;
const int maxn = 200005; int n,m, id, low[maxn], timestamp[maxn], t, cnt, res, ebccnum, belong[maxn];
struct Edge { int to, id; Edge(int to, int id):to(to),id(id){} }; vector<Edge> g[maxn]; typedef vector<Edge>::iterator vit; stack<int> s; void dfs(int cur, int fa) { timestamp[cur] = low[cur] = ++t; s.push(cur); for (vit x = g[cur].begin(); x!=g[cur].end(); x++) { if (!timestamp[x->to]) { dfs(x->to, x->id); low[cur] = min(low[cur], low[x->to]); if (low[x->to]>timestamp[cur]) { cnt++; ebccnum++; int j; do { j = s.top(); s.pop(); belong[j] = ebccnum; } while (j!=x->to); } } else if (x->id!=fa) { low[cur] = min(low[cur], timestamp[x->to]); } } }
int head[maxn],num; struct Arc { int from, to, nxt; }gg[2000005];
void addArc(int a, int b) { gg[num].from = a, gg[num].to = b, gg[num].nxt = head[a]; head[a] = num++; }
bool v[maxn]; int bfs(int i, int &ans) { memset(v, 0, sizeof(v)); ans = 0; int p; struct Node { int to, dis; Node(int to, int dis):to(to),dis(dis){} }; queue<Node> q; q.push(Node(i,0)); v[i] = true; while(!q.empty()) { Node front = q.front(); q.pop(); if (ans<front.dis) { ans = front.dis; p = front.to; } for (int h = head[front.to],to; ~h; h = gg[h].nxt) { to = gg[h].to; if (!v[to]) { v[to] = true; q.push(Node(to, front.dis+1)); } } } return p; }
void rebuild() { memset(head, -1, sizeof(head)); num = 0; for (int i = 1; i<=n;i++) { for (vit x = g[i].begin(); x!=g[i].end(); x++) { if (belong[i]!=belong[x->to]) { addArc(belong[i], belong[x->to]); } } } }
int main() { #ifdef LOCAL freopen("d:\\data.in", "r", stdin); #endif while(scanf("%d%d", &n,&m),n||m) { id = t = cnt = 0; memset(low,0, sizeof(low)); memset(timestamp, 0, sizeof(timestamp)); for (int i = 1; i<=n;i++) { g[i].clear(); } while(m--) { int a,b; scanf("%d%d", &a, &b); id++; g[a].push_back(Edge(b,id)), g[b].push_back(Edge(a, id)); } ebccnum = 0; while(!s.empty()) { s.pop(); } memset(belong, 0, sizeof(belong)); dfs(1,0); if (!ebccnum) { puts("0"); continue; } ebccnum++; for (int i = 1; i<=n;i++) { if (!belong[i]) { belong[i] = ebccnum; } } rebuild(); int ans; int p = bfs(1, ans); bfs(p, ans); printf("%d\n", cnt-ans); } return 0; }
|