二分图不能有奇环
那么这条边一定在奇环的交上 还不能在偶环上 因为偶环和奇环会组成新的奇环
那么给奇环打+1标记 偶环打-1标记 标记为奇环个数的边即可
这种打标记的树D方法比较重要
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define cl(x) memset(x,0,sizeof(x))
using namespace std;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(int &x){
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
const int N=1000005;
struct edge{
int u,v,idx,next;
}G[N<<1];
int head[N],inum=1;
inline void add(int u,int v,int idx,int p){
G[p].u=u; G[p].v=v; G[p].idx=idx; G[p].next=head[u]; head[u]=p;
}
#define V G[p].v
int n,m;
int vst[N],depth[N];
int tag[N],cnt,tmp;
int cir,totc;
void dfs(int u,int fa){
vst[u]=1;
for (int p=head[u];p;p=G[p].next)
if (p!=(fa^1))
if(!vst[V])
depth[V]=depth[u]+1,dfs(V,p),tag[u]+=tag[V];
else{
if(depth[V]>depth[u])
continue;
if((depth[u]-depth[V]+1)&1)
tag[u]++,tag[V]--,cnt++,tmp=G[p].idx;
else
tag[u]--,tag[V]++;
}
}
int ans[N],tot,cur;
inline void find(int u,int fa){
vst[u]=1;
if (tag[u]==cnt)
ans[++tot]=G[fa].idx;
for (int p=head[u];p;p=G[p].next)
if (!vst[V])
find(V,p);
}
int main(){
int iu,iv;
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(n); read(m);
for (int i=1;i<=m;i++){
read(iu),read(iv);
if (iu==iv)
cir=i,totc++;
else
add(iu,iv,i,++inum),add(iv,iu,i,++inum);
}
for (int i=1;i<=n;i++)
if (!vst[i])
depth[i]=1,dfs(i,0);
if (cnt==0){
if (totc){
if (totc==1)
printf("1\n%d\n",cir);
else
printf("0\n");
}else{
printf("%d\n",m);
for (int i=1;i<=m;i++)
printf("%d%c",i,i==m?'\n':' ');
}
return 0;
}
if (totc)
return printf("0\n"),0;
cl(vst);
for (int i=1;i<=n;i++)
if (!vst[i])
find(i,0);
if (cnt==1) ans[++tot]=tmp;
sort(ans+1,ans+tot+1);
printf("%d\n",tot);
for (int i=1;i<=tot;i++)
printf("%d%c",ans[i],i==tot?'\n':' ');
return 0;
}