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

点双连通分量F. Simple Cycles Edges的更多相关文章

  1. Educational Codeforces Round 42 (Rated for Div. 2)F - Simple Cycles Edges

    http://codeforces.com/contest/962/problem/F 求没有被两个及以上的简单环包含的边 解法:双联通求割顶,在bcc中看这是不是一个简单环,是的话把整个bcc的环加 ...

  2. codeforces 962 F Simple Cycles Edges

    求简单环,即求点=边数的点双分量,加上判断点和边的模板即可 (简单环模板,区分与点双缩点) ; ], edgecnt, dfn[maxm], low[maxm], bcc_cnt, bccnum[ma ...

  3. CF962F Simple Cycles Edges

    CF962F Simple Cycles Edges 给定一个连通无向图,求有多少条边仅被包含在一个简单环内并输出 \(n,\ m\leq10^5\) tarjan 首先,一个连通块是一个环,当且仅当 ...

  4. Codeforces962F Simple Cycles Edges 【双连通分量】【dfs树】

    题目大意: 给出一个无向图,问有哪些边只属于一个简单环. 题目分析: 如果这道题我们掌握了点双连通分量,那么结论会很显然,找到每个点双,如果一个n个点的点双正好由n条边构成,那么这些边都是可以的. 这 ...

  5. Simple Cycles Edges CodeForces - 962F(点双连通分量)

    题意: 求出简单环的所有边,简单环即为边在一个环内 解析: 求出点双连通分量,如果一个连通分量的点数和边数相等,则为一个简单环 点双连通分量  任意两个点都至少存在两条点不重复的路径  即任意两条边都 ...

  6. codeforces 962F.simple cycle(tarjan/点双连通分量)

    题目连接:http://codeforces.com/contest/962/problem/F 题目大意是定义一个simple cycle为从一个节点开始绕环走一遍能经过simple cycle内任 ...

  7. HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...

  8. hdu-4612-Warm up(边双连通分量--有重边)

    题意:有N 个点,M条边,加一条边,求割边最少.(有重边) 分析:先求双连通分量,缩点形成一个生成树,然后求这个的直径,割边-直径即是答案 因为有的图上可能有重边,这样不好处理.我们记录每条边的标号( ...

  9. 双连通分量(点-双连通分量&边-双连通分量)

    概念: 双连通分量有点双连通分量和边双连通分量两种.若一个无向图中的去掉任意一个节点(一条边)都不会改变此图的连通性,即不存在割点(桥),则称作点(边)双连通图. 一个无向图中的每一个极大点(边)双连 ...

随机推荐

  1. QT bug ig9icd64.dll

    QT bug ig9icd64.dll bugintel ig9icd64.dll 处有未经处理的异常 遇到了一个 奇奇怪怪的bug, 一般的QT程序中 在main.cpp 中初始化一个窗口进行显示后 ...

  2. Linux系统管理第三次作业 账号管理 权限及归属管理

    1.创建/guanli 目录,在/guanli下创建zonghe 和 jishu 两个目录(一条命令) [root@localhost ~]# mkdir /guanli [root@localhos ...

  3. UVALive 7505 Hungry Game of Ants

    1. 笔记 比较容易的动态规划题.往左很好考虑,往右用dpi表示前i只都被k吃掉后,k继续往右仍然不死的情况数.状态转移方程为dp[I]=dp[I+1]+...+dp[j],分别对应第I+1位向左,. ...

  4. css之Grid Layout详解

    css之Grid Layout详解 CSS Grid Layout擅长将页面划分为主要区域,或者在从HTML基元构建的控件的各个部分之间定义大小,位置和图层之间的关系. 与表格一样,网格布局使作者能够 ...

  5. c语言-----劫持系统03

    1. 回顾 在前2节我们已经实现了劫持原理.函数指针等一些概念,下面进行系统劫持 2. 工具 vs2017 Detours 3. windows如何创建一个进程? (1)创建进程函数 CreatePr ...

  6. MyBatis学习总结(9)——使用MyBatis Generator自动创建代码

    2019独角兽企业重金招聘Python工程师标准>>> 由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所 ...

  7. POJ 1330 Nearest Common Ancestors(裸LCA)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39596   Accept ...

  8. 图论--最短路-- Dijkstra模板(目前见到的最好用的)

    之前的我那个板子,老是卡内存,不知道为什么,我看别人过的那个题都是结构体,我就开始对自己板子做了修改,然后他奶奶的就过了,而且速度也提高了,内存也小了.(自从用了这个板子,隔壁小孩馋哭了)也不知道为啥 ...

  9. Java笔记(day20-22)

    IO流: 输入流.输出流 字节流.字符流:为了处理文字数据方便而出现的对象. (其实这些对象的内部使用的还是字节流(因为文字最终也是字节数据,只不过,通过字节流读取了相对应的字节数,没有对这些字节直接 ...

  10. MySQL(二)MySQL中的存储引擎

    前言 数据库存储引擎是数据库底层软件组织,数据库管理系统(DBMS)使用数据引擎进行创建.查询.更新和删除数据.不同的存储引擎提供不同的存储机制.索引技巧.锁定水平等功能,使用不同的存储引擎,还可以 ...