题意其实就是砍一条边,使得砍完后图中没有奇环,且不能砍偶环上的边
所以,建一棵dfs树,找有多少条边同时在所有奇环上且不在偶环上- -
code:
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = 110000;
const int maxm = 210000;
struct edge
{
int y,nex;
edge(){}
edge(int _y,int _nex){y=_y;nex=_nex;}
}a[maxm<<1]; int len,fir[maxn];
void ins(int x,int y){a[++len]=edge(y,fir[x]); fir[x]=len;}
int n,m,s;
int s1[maxn],s2[maxn];
int d[maxn],t[maxn],tp;
bool v[maxn];
void dfs(int x,int pre)
{
v[x]=true; t[++tp]=x;
for(int k=fir[x];k;k=a[k].nex)
{
if((k^1)==pre) continue;
int y=a[k].y;
if(!v[y])
{
d[y]=d[x]+1;
dfs(y,k);
s1[x]+=s1[y];
s2[x]+=s2[y];
}
else if(d[x]>d[y])
{
if((d[x]-d[y])&1)
{
s2[x]++;
s2[y]--;
}
else
{
s++;
s1[x]++;
s1[y]--;
}
}
}
}
int main()
{
len=1;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y; scanf("%d%d",&x,&y);
ins(x,y); ins(y,x);
}
int la=0,re=0;
for(int i=1;i<=n;i++)
if(!v[i])
{
s=tp=0;
dfs(i,0);
if(s)
{
re=0;
if(la) break;
}
if(s==1) re++;
if(!la)
{
for(int j=2;j<=tp;j++)
if(s1[t[j]]==s&&!s2[t[j]]) re++;
}
if(s) la=i;
}
printf("%d\n",re);
return 0;
}