点双连通分量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).

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.

Determine the edges, which belong to exactly on one simple cycle.

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.

Each of the following mm lines contain two integers uu and vv (1u,vn1≤u,v≤n, uvu≠v) — the description of the edges.

Output

In the first line print the number of edges, which belong to exactly one simple cycle.

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.

 

题意:问你给的m条边里面有几条是只属于一个简单环的。

可以求点连通分量,如果点连通分量里面点的数目==边的数目即可。

至于为什么不能用边连通分量,是因为边双连通不能处理一个点既是一个环的组成部分又是另外一个环的组成部分

 

顺便找到了两个还算可以的模板的样子:模板一  模板二

#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]);
}

 

posted @ 2018-04-17 22:05  Billyshuai  阅读(491)  评论(0编辑  收藏  举报