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 (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.

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.

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=;
int n,m,x,y,low[N],dfn[N],cnt;
int q[N],l,H[N],to[N<<],nxt[N<<],tot=;
int bl[N],scnt;
bool vis[N<<];
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^]=;
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=,bnum=;
++scnt;
do{
t=q[--l];
if(bl[to[t]]!=scnt) bl[to[t]]=scnt,++num;
if(bl[to[t^]]!=scnt) bl[to[t^]]=scnt,++num;
a[++bnum]=t;
}while(t!=i);
if(num==bnum) for(int i=;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=;i<=m;++i) {
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
for(int i=;i<=n;++i) if(!dfn[i]) dfs(i,);
sort(A+,A+ans+);
printf("%d\n",ans);
for(int i=;i<=ans;++i) printf("%d ",A[i]>>);
}

边连通是不可行的,模板备用.

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+;
int dfn[N],low[N],H[N],nxt[N<<],to[N<<];
int n,m,x,y,cnt,tot=;
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=;
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^]=;
if(low[v]>=dfn[u]) is[u]=;
}
else low[u]=min(low[u],dfn[v]);
}
if(chi==&&fa==-) is[u]=;
}
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^]||v==fa) continue;
ib[i]=ib[i^]=;
a[++bnum]=i>>;
if(!dfn[v]) dfn[v]=,dfs2(v,u);
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=m;++i) {
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
for(int i=;i<=n;++i) if(!dfn[i]) dfs(i,-);
for(int i=;i<=n;++i) dfn[i]=;
for(int i=;i<=n;++i) if(!dfn[i]) {
num=bnum=;
dfn[i]=;
dfs2(i,-);
if(num==bnum) for(int j=;j<=num;++j) A[++ans]=a[j];
}
printf("%d\n",ans);
sort(A+,A+ans+);
for(int i=;i<=ans;++i) printf("%d ",A[i]);
}

最新文章

  1. Ubuntu 16.10 安装byzanz截取动态效果图工具
  2. Windbg Extension NetExt 使用指南 【1】 ---- NetExt 介绍
  3. Canvas——使用定时器模拟动态加载动画!
  4. Python - 类与对象的方法
  5. 回答阿里社招面试如何准备,顺便谈谈对于Java程序猿学习当中各个阶段的建议
  6. gRPC+etcd的优势分析
  7. Hello World!
  8. 【Effective Java】11、同步访问共享的可变数据
  9. 异步框架asyn4j的原理
  10. Webproject 每次运行都停到workerDone(this); tomcat调试
  11. 【转】ubuntu 11.10(32位系统)下编译android源码
  12. Java Socket编程基础(1)
  13. Android 7.0 Power 按键处理流程
  14. JSF生命周期&amp;Facelets的生命周期
  15. Django_博客项目 注册用户引发 ValueError: The given username must be set
  16. 五步整理你的css文件
  17. 微信小程序底部导航Tabbar
  18. UOJ 310 黎明前的巧克力(FWT)
  19. UITextView in iOS7 doesn&amp;#39;t scroll
  20. Java clone() 浅拷贝 深拷贝

热门文章

  1. 在java 8 stream表达式中实现if/else逻辑
  2. Vue Cli 3 打包上线 静态资源404问题解决方案
  3. memcached 原子性操作 CAS模式
  4. Acmer 仅以此纪念最痛苦的一天
  5. CF--思维练习-- CodeForces - 215C - Crosses(思维题)
  6. 2019 Multi-University Training Contest 10 I Block Breaker
  7. 图论--树的直径--DFS+树形DP模板
  8. Nginx编译与安装
  9. JUC之CAS
  10. Coursera课程笔记----Write Professional Emails in English----Week 1