CF962F Simple Cycles Edges

给定一个连通无向图,求有多少条边仅被包含在一个简单环内并输出

\(n,\ m\leq10^5\)

tarjan


首先,一个连通块是一个环,当且仅当该连通块的 点数=边数

可以发现,如果两个环仅由一个公共点连接,那么这两个环互不影响,即点双两两互不影响。

所以我们可以考虑处理出点双和每个点双内的边数

但是求出点双后暴力dfs会被如下数据卡掉:

66667 99999
1 2
1 3
2 3
1 4
1 5
4 5
1 6
1 7
6 7
...

因为 \(1\) 节点每次枚举所有边的效率太低

于是可以在tarjan时将所有边压入栈中,再用set统计点双中的边数以及答案并去重

时间复杂度 \(O(n+m)\)

代码

#include <bits/stdc++.h>
using namespace std; #define nc getchar()
const int maxn = 1e5 + 10;
int n, m, tot, top, h[maxn], dfn[maxn], low[maxn], st[maxn * 3];
struct edges {
int nxt, to;
} e[maxn << 1];
set <int> ans, edge[maxn], node[maxn]; inline int read() {
int x = 0; char c = nc;
while (c < 48) c = nc;
while (c > 47) x = x * 10 + c - 48, c = nc;
return x;
} void addline(int u, int v) {
static int cnt = 1;
e[++cnt] = edges{h[u], v}, h[u] = cnt;
} void tarjan(int u, int f) {
static int now;
dfn[u] = low[u] = ++now;
for (int i = h[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (!dfn[v]) {
st[++top] = i >> 1, st[++top] = u, st[++top] = v;
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (dfn[u] <= low[v]) {
tot++;
while (1) {
int t1, t2;
node[tot].insert(t1 = st[top--]);
node[tot].insert(t2 = st[top--]);
edge[tot].insert(st[top--]);
if (t1 == v && t2 == u) break;
}
}
} else if (dfn[v] < dfn[u] && v != f) {
st[++top] = i >> 1, st[++top] = u, st[++top] = v;
low[u] = min(low[u], dfn[v]);
}
}
} int main() {
n = read(), m = read();
for (int i = 1; i <= m; i++) {
int u = read(), v = read();
addline(u, v), addline(v, u);
}
for (int i = 1; i <= n; i++) {
if (!dfn[i]) tarjan(i, 0);
}
for (int i = 1; i <= tot; i++) {
if (edge[i].size() == node[i].size()) {
ans.insert(edge[i].begin(), edge[i].end());
}
}
printf("%d\n", (int)ans.size());
for (int u : ans) printf("%d ", u);
return 0;
}

CF962F Simple Cycles Edges的更多相关文章

  1. 点双连通分量F. Simple Cycles Edges

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

  2. codeforces 962 F Simple Cycles Edges

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

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

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

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

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

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

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

  6. [CodeForces 11D] A Simple Task - 状态压缩入门

    状态压缩/Bitmask 在动态规划问题中,我们会遇到需要记录一个节点是否被占用/是否到达过的情况.而对于一个节点数有多个甚至十几个的问题,开一个巨型的[0/1]数组显然不现实.于是就引入了状态压缩, ...

  7. CodeForces - 11D A Simple Task

    Discription Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycl ...

  8. Codeforces C. A Simple Task(状态压缩dp)

    题目描述:  A Simple Task time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. POJ 3895 Cycles of Lanes (dfs)

    Description Each of the M lanes of the Park of Polytechnic University of Bucharest connects two of t ...

随机推荐

  1. 08-HTML-框架标签

    <html> <head>  <title>框架标签学习</title>  <meta charset="utf-8"/> ...

  2. Ambari架构源码解析

    1. Ambari介绍 Apache Ambari是一种基于Web的工具,支持Apache Hadoop集群的供应.管理和监控.Ambari已支持大多数Hadoop组件,包括HDFS.MapReduc ...

  3. 使用SQL查看表字段和字段说明

    MySql: show full columns from tableName; Sql server: SELECT A.name AS table_name, B.name AS column_n ...

  4. Android 自定义弹出框带EditText

    EditText 布局页面 edittext_ownername_dialog.xml: <?xml version="1.0" encoding="utf-8&q ...

  5. (后端)JackSon将java对象转换为JSON字符串(转)

    转载小金金金丶园友: JackSon可以将java对象转换为JSON字符串,步骤如下: 1.导入JackSon 的jar包 2.创建ObjectMapper对象 3.使用ObjectMapper对象的 ...

  6. (后端)spring的@Transactional注解详细用法(转)

    转自一人浅醉-博客园: 事务隔离级别 隔离级别是指若干个并发的事务之间的隔离程度.TransactionDefinition 接口中定义了五个表示隔离级别的常量: TransactionDefinit ...

  7. SAP 销售条件表增强栏位

    有时遇到一个比较特殊的业务,比如公司间免费订单,既要让价格为0,不读取VK11里创建的价格, 又要让公司间的价格读取VK11,这实际上是有矛盾的,也就是说一个订单里面的两行,物料一样,客户一样,就会出 ...

  8. VMware虚拟机CentOS7网络通信与无线上网

    实现主机和虚拟机网络通信 1.虚拟机设置 VMware界面最上面,选择[虚拟机]->[设置]:将网络连接改为"桥接模式",如下图所示: 2.CentOS7网络设置 自动获取I ...

  9. OneAPM 重磅登陆 CTDC 2018展示“ AIOps 双雄”创新实力

    9月7日-8日,以“AI 智享未来”为主题的2018第二届 CTDC 首席技术官领袖峰会在乌镇盛大召开,大会由 CTOA 首席技术官领袖联盟.ITShare 主办,大会邀请了国内外顶级互联网.在线教育 ...

  10. Visual Studio Team Services 动手实验

    Visual Studio Team Services 动手实验 概述 为Visual Studio Team Services提供的动手实验,要完成实验首先需要满足以下条件: Visual Stud ...