点双连通份量F. Simple Cycles Edges

F. Simple Cycles Edges
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

Input

The first line contain two integers nn and mm (1n100000(1≤n≤100000, 0mmin(n(n1)/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 (1u,vn1≤u,v≤n, uvu≠v) — the description of the edges.code

Output

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]); }
相关文章
相关标签/搜索