#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int maxn=1000+5;
const int maxm=maxn*maxn;
struct Edge{
int u,v;
Edge(int u=0,int v=0):u(u),v(v){}
}e[maxm];
int n,m,tot,stamp,dfn[maxn],low[maxn],bccno[maxn],bcc_cnt,de[maxn];
vector<int> vec[maxn],bcc[maxn];
bool g[maxn][maxn],isbridge[maxm];
void tarjan(int index,int fa)
{
int tmp;
dfn[index]=low[index]=++stamp;
for(int i=0;i<vec[index].size();i++)
{
tmp=e[vec[index][i]].v;
if(!dfn[tmp])
{
tarjan(tmp,index);
low[index]=min(low[index],low[tmp]);
if(low[tmp]>dfn[index])
isbridge[vec[index][i]]=isbridge[vec[index][i]^1]=1;
}
else if(dfn[tmp]<dfn[index] && tmp!=fa)
{
low[index]=min(low[index], dfn[tmp]);
}
}
}
void dfs(int index)
{
dfn[index]=1;
bccno[index]=bcc_cnt;
for(int i=0;i<vec[index].size();i++)
{
int tmp=vec[index][i];
if(isbridge[tmp])
continue;
if(!dfn[e[tmp].v])
{
dfs(e[tmp].v);
}
}
}
void find_ebcc(){
bcc_cnt=stamp=0;
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(isbridge,0,sizeof(isbridge));
memset(bccno,0,sizeof(bccno));
memset(bcc,0,sizeof(bcc));
for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i, -1);
memset(dfn,0,sizeof(dfn));
for(int i=1;i<=n;i++)
{
if(!dfn[i])
{
bcc_cnt++;
dfs(i);
}
}
}
void addedge(int u,int v)
{
e[tot]=Edge(u,v);
vec[u].push_back(tot++);
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
tot=0;
memset(vec,0,sizeof(vec));
for(int i=0;i<m;i++){
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
find_ebcc();
memset(de,0,sizeof(de));
for(int i=0;i<tot;i++){
if(bccno[e[i].u]!=bccno[e[i].v]) {de[bccno[e[i].u]]++;}
}
int ans=0;
for(int i=1;i<=bcc_cnt;i++){
if(de[i]==1) ans++;
if(de[i]==0) ans+=2;
}
if(bcc_cnt==1) printf("0\n");
else printf("%d\n",(ans+1)/2);
}
return 0;
}
UVA 10972 RevolC FaeLoN(边双联通+缩点)
最新推荐文章于 2020-02-01 18:16:00 发布