1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std; inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } bool bj=0,Dist[1000005]; struct Edge { int from,to; } edge[1000005]; int n,m,father[1000005],depth[1000005],top=0,ans=0,Ans[1000005]; struct St { int x,y,hx,hy; } S[1000005]; int Get_Father(int x) { if(father[x]==x)return x; else return Get_Father(father[x]); } int Get_Dist(int x) { if(father[x]==x)return 0; else return Get_Dist(father[x])^Dist[x]; } void Merge(int id) { if(bj)return; Edge& e=edge[id]; int fx=Get_Father(e.from),fy=Get_Father(e.to),dx=Get_Dist(e.from),dy=Get_Dist(e.to); if(fx==fy) { if(dx==dy)bj=1; return; } if(depth[fx]<depth[fy])swap(fx,fy); S[++top]= (St) { fx,fy,depth[fx],depth[fy] }; father[fy]=fx; Dist[fy]=1^dx^dy; if(depth[fx]==depth[fy])depth[fx]++; } void Cut(int id) { father[S[id].x]=S[id].x; depth[S[id].x]=S[id].hx; Dist[S[id].x]=0; father[S[id].y]=S[id].y; depth[S[id].y]=S[id].hy; Dist[S[id].y]=0; } void CDQBinary(int Left,int Right) { if(Left==Right) { if(!bj)Ans[++ans]=Left; return; } int mid=(Left+Right)>>1,tmptop=top,tmpbj=bj; for(int i=mid+1; i<=Right; i++)Merge(i); if(!bj)CDQBinary(Left,mid); bj=tmpbj; while(top>tmptop) { Cut(top); top--; } for(int i=Left; i<=mid; i++)Merge(i); if(!bj)CDQBinary(mid+1,Right); bj=tmpbj; while(top>tmptop) { Cut(top); top--; } } int main() { n=Get_Int(); m=Get_Int(); if(m==0) { puts("0"); return 0; } for(int i=1; i<=m; i++) { edge[i].from=Get_Int(); edge[i].to=Get_Int(); } for(int i=1; i<=n; i++) { father[i]=i; depth[i]=1; } CDQBinary(1,m); printf("%d\n",ans); for(int i=1; i<=ans; i++)printf("%d%c",Ans[i],i==ans?'\n':' '); return 0; }
|