依题意:对电阻X,若可以选择使X不流过电流,当且仅当所有奇环都包含X且所有偶环都不包含X。
利用Dfs搜索树,如果点a搜到的边的端点b已经走过,说明点b为a的祖先,利用深度差可求得环上点的个数。
#include<iostream>
#include<cstdio>
#define N 100005
#define M 400005
using namespace std;
int n,m,l=1,Ans,tot[2],tmp;bool vis[N];
int fa[N],first[N],next[M],to[M],sum[N][2],dep[N];
inline int read()
{
char c=getchar();int x=0;
while(c<'0'||c>'9')c=getchar();
while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
return x;
}
inline void link(int x,int y)
{
to[++l]=y;next[l]=first[x];first[x]=l;
to[++l]=x;next[l]=first[y];first[y]=l;
}
void Dfs(int t)
{
vis[t]=1;
for(int i=first[t];i;i=next[i])
if ( (i^1)!=fa[t] )
{
if (vis[to[i]])
{
if (dep[to[i]]>dep[t])continue;
tmp=(dep[t]-dep[to[i]])&1;
sum[t][tmp]++;sum[to[i]][tmp]--;tot[tmp]++;
}
else
{
fa[to[i]]=i;dep[to[i]]=dep[t]+1;
Dfs(to[i]);
sum[t][0]+=sum[to[i]][0];
sum[t][1]+=sum[to[i]][1];
}
}
}
int main()
{
n=read();m=read();
for(int i=1;i<=m;i++)link(read(),read());
for (int i=1;i<=n;i++)if(!vis[i]) Dfs(i);
for (int i=1;i<=n;i++)if (sum[i][0]==tot[0]&&!sum[i][1]&&fa[i]) Ans++;
if (tot[0]==1)Ans++;
printf("%d\n",Ans);
return 0;
}