You are given an undirected graph, consisting of nn vertices and mm edges. The graph does not necessarily connected. Guaranteed, that the graph does not contain multiple edges (more than one edges between a pair of vertices) or loops (edges from a vertex to itself).oop
A cycle in a graph is called a simple, if it contains each own vertex exactly once. So simple cycle doesn't allow to visit a vertex more than once in a cycle.url
Determine the edges, which belong to exactly on one simple cycle.spa
The first line contain two integers nn and mm (1≤n≤100000(1≤n≤100000, 0≤m≤min(n⋅(n−1)/2,100000))0≤m≤min(n⋅(n−1)/2,100000)) — the number of vertices and the number of edges..net
Each of the following mm lines contain two integers uu and vv (1≤u,v≤n1≤u,v≤n, u≠vu≠v) — the description of the edges.code
In the first line print the number of edges, which belong to exactly one simple cycle.blog
In the second line print the indices of edges, which belong to exactly one simple cycle, in increasing order. The edges are numbered from one in the same order as they are given in the input.ip
題意:問你給的m條邊裏面有幾條是隻屬於一個簡單環的。ci
能夠求點連通份量,若是點連通份量裏面點的數目==邊的數目便可。get
至於爲何不能用邊連通份量,是由於邊雙連通不能處理一個點既是一個環的組成部分又是另一個環的組成部分input
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=100010; int n,m,x,y,low[N],dfn[N],cnt; int q[N],l,H[N],to[N<<1],nxt[N<<1],tot=1; int bl[N],scnt; bool vis[N<<1]; int a[N],A[N],ans; void add(int x,int y){ to[++tot]=y;nxt[tot]=H[x];H[x]=tot; } void dfs(int x,int y){ dfn[x]=low[x]=++cnt; for(int i=H[x];i;i=nxt[i]){ if(to[i]==y||vis[i]) continue; vis[i]=vis[i^1]=1; q[l++]=i; int v=to[i]; if(!dfn[v]){ dfs(v,x); low[x]=min(low[x],low[v]); if(dfn[x]<=low[v]) { int t,num=0,bnum=0; ++scnt; do{ t=q[--l]; if(bl[to[t]]!=scnt) bl[to[t]]=scnt,++num; if(bl[to[t^1]]!=scnt) bl[to[t^1]]=scnt,++num; a[++bnum]=t; }while(t!=i); if(num==bnum) for(int i=1;i<=bnum;++i) A[++ans]=a[i]; } } else low[x]=min(low[x],dfn[v]); } } int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=m;++i) { scanf("%d%d",&x,&y); add(x,y); add(y,x); } for(int i=1;i<=n;++i) if(!dfn[i]) dfs(i,0); sort(A+1,A+ans+1); printf("%d\n",ans); for(int i=1;i<=ans;++i) printf("%d ",A[i]>>1); }
邊連通是不可行的,模板備用.
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=1e5+88; int dfn[N],low[N],H[N],nxt[N<<1],to[N<<1]; int n,m,x,y,cnt,tot=1; bool ib[N],is[N]; void add(int x,int y){ to[++tot]=y;nxt[tot]=H[x];H[x]=tot; } void dfs(int u,int fa){ low[u]=dfn[u]=++cnt; int chi=0; for(int i=H[u];i;i=nxt[i]){ int v=to[i]; if(v==fa) continue; if(!dfn[v]) { ++chi; dfs(v,u); low[u]=min(low[u],low[v]); if(low[v]>dfn[u]) ib[i]=ib[i^1]=1; if(low[v]>=dfn[u]) is[u]=1; } else low[u]=min(low[u],dfn[v]); } if(chi==1&&fa==-1) is[u]=0; } int num,bnum,a[N],A[N],ans; void dfs2(int u,int fa){ ++num;for(int i=H[u];i;i=nxt[i]) { int v=to[i]; if(ib[i]||ib[i^1]||v==fa) continue; ib[i]=ib[i^1]=1; a[++bnum]=i>>1; if(!dfn[v]) dfn[v]=1,dfs2(v,u); } } int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=m;++i) { scanf("%d%d",&x,&y); add(x,y); add(y,x); } for(int i=1;i<=n;++i) if(!dfn[i]) dfs(i,-1); for(int i=0;i<=n;++i) dfn[i]=0; for(int i=1;i<=n;++i) if(!dfn[i]) { num=bnum=0; dfn[i]=1; dfs2(i,-1); if(num==bnum) for(int j=1;j<=num;++j) A[++ans]=a[j]; } printf("%d\n",ans); sort(A+1,A+ans+1); for(int i=1;i<=ans;++i) printf("%d ",A[i]); }