首先tarjan把边双都缩成一个点,这样我们就得到了一棵树。
如何把这棵树加最少的边变成边双呢?
统计出树中度为1的节点的个数,即为叶节点的个数,记为leaf。则至少在树上添加(leaf+1)/2条边,就能使树达到边二连通,所以至少添加的边数就是(leaf+1)/2。具体方法为,首先把两个最近公共祖先最远的两个叶节点之间连接一条边,这样可以把这两个点到祖先的路径上所有点收缩到一起,因为一个形成的环一定是双连通的。然后再找两个最近公共祖先最远的两个叶节点,这样一对一对找完,恰好是(leaf+1)/2次,把所有点收缩到了一起。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 5010
inline char gc(){
static char buf[1<<16],*S,*T;
if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
return x*f;
}
int n,m,h[N],num=1,dfn[N],low[N],dfnum=0,scc,bel[N],du[N],ans=0;
bool used[N<<1];
struct edge{
int to,next;
}data[N<<2];
stack<int>qq;
inline void tarjan(int x){
dfn[x]=low[x]=++dfnum;qq.push(x);
for(int i=h[x];i;i=data[i].next){
if(used[i>>1]) continue;int y=data[i].to;used[i>>1]=1;
if(!dfn[y]) tarjan(y),low[x]=min(low[x],low[y]);
else low[x]=min(low[x],dfn[y]);
}if(dfn[x]==low[x]){
++scc;while(1){
int y=qq.top();qq.pop();bel[y]=scc;
if(y==x) break;
}
}
}
int main(){
// freopen("a.in","r",stdin);
n=read();m=read();
for(int i=1;i<=m;++i){
int x=read(),y=read();
data[++num].to=y;data[num].next=h[x];h[x]=num;
data[++num].to=x;data[num].next=h[y];h[y]=num;
}tarjan(1);
for(int x=1;x<=n;++x)
for(int i=h[x];i;i=data[i].next){
int y=data[i].to;if(bel[x]!=bel[y]) ++du[bel[x]];
}
for(int i=1;i<=scc;++i) if(du[i]==1) ++ans;
printf("%d\n",ans+1>>1);
return 0;
}