题目大意
给定一个无向连通图,问至少加几条边使得图成为边双联通图
样例输入
Sample Input 1
10 12
1 2
1 3
1 4
2 5
2 6
5 6
3 7
3 8
7 8
4 9
4 10
9 10
Sample Input 2
3 3
1 2
2 3
1 3
样例输出
Output for Sample Input 1
2
Output for Sample Input 2
0
Solution
Tarjan将所有边双联通分量缩点以后,形成一棵树
答案就是(树上度数等于一的点的个数+1)>>1
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 1000010
inline int read(){
int ret=0;
char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9')ret=ret*10+ch-'0',ch=getchar();
return ret;
}
struct Edge{
int u,v,next;
}E[maxn];
int ecnt=0,head[maxn],dfn[maxn],low[maxn],deg[maxn],ord,vis[maxn];
void addedge(int u,int v){
E[++ecnt].u=u;
E[ecnt].v=v;
E[ecnt].next=head[u];
head[u]=ecnt;
}
void Tarjan(int x,int fx){
dfn[x]=low[x]=++ord;
vis[x]=1;
for(int i=head[x];i;i=E[i].next){
int v=E[i].v;
if(v==fx) continue;
if(dfn[v]==0) Tarjan(v,x),low[x]=min(low[x],low[v]);
if(vis[v]==1) low[x]=min(dfn[v],low[x]);
}
}
void init(){
memset(E,0,sizeof(E));
for(int i=0;i<maxn;++i) head[i]=dfn[i]=low[i]=deg[i]=vis[i]=0;
ecnt=0,ord=0;
}
int main(){
//freopen("poj3352.in","r",stdin);
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
init();
int ans=0;
for(int i=1;i<=m;++i){
int aa=read(),bb=read();
addedge(aa,bb),addedge(bb,aa);
}
for(int i=1;i<=n;++i){
if(dfn[i]==0) Tarjan(i,0);
}
for(int i=1;i<=ecnt;++i){
if(low[E[i].u]!=low[E[i].v])++deg[low[E[i].u]];
}
for(int i=1;i<=n;++i) if(deg[i]==1) ++ans;
printf("%d\n",(ans+1)>>1);
}
return 0;
}