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的更多相关文章
- 点双连通分量F. Simple Cycles Edges
F. Simple Cycles Edges time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- codeforces 962 F Simple Cycles Edges
求简单环,即求点=边数的点双分量,加上判断点和边的模板即可 (简单环模板,区分与点双缩点) ; ], edgecnt, dfn[maxm], low[maxm], bcc_cnt, bccnum[ma ...
- Codeforces962F Simple Cycles Edges 【双连通分量】【dfs树】
题目大意: 给出一个无向图,问有哪些边只属于一个简单环. 题目分析: 如果这道题我们掌握了点双连通分量,那么结论会很显然,找到每个点双,如果一个n个点的点双正好由n条边构成,那么这些边都是可以的. 这 ...
- Simple Cycles Edges CodeForces - 962F(点双连通分量)
题意: 求出简单环的所有边,简单环即为边在一个环内 解析: 求出点双连通分量,如果一个连通分量的点数和边数相等,则为一个简单环 点双连通分量 任意两个点都至少存在两条点不重复的路径 即任意两条边都 ...
- Educational Codeforces Round 42 (Rated for Div. 2)F - Simple Cycles Edges
http://codeforces.com/contest/962/problem/F 求没有被两个及以上的简单环包含的边 解法:双联通求割顶,在bcc中看这是不是一个简单环,是的话把整个bcc的环加 ...
- [CodeForces 11D] A Simple Task - 状态压缩入门
状态压缩/Bitmask 在动态规划问题中,我们会遇到需要记录一个节点是否被占用/是否到达过的情况.而对于一个节点数有多个甚至十几个的问题,开一个巨型的[0/1]数组显然不现实.于是就引入了状态压缩, ...
- CodeForces - 11D A Simple Task
Discription Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycl ...
- Codeforces C. A Simple Task(状态压缩dp)
题目描述: A Simple Task time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- POJ 3895 Cycles of Lanes (dfs)
Description Each of the M lanes of the Park of Polytechnic University of Bucharest connects two of t ...
随机推荐
- Human Motion Analysis with Wearable Inertial Sensors——阅读1
Human Motion Analysis with Wearable Inertial Sensors——阅读 博主认为对于做室内定位和导航的人这是一篇很很棒的文章,不是他的技术很牛,而是这是一篇医 ...
- Nginx 部署多个 web 项目(虚拟主机)
1,创建 www 目录 2,在 www 目录下创建 test1 和 test2 目录,表示两个项目 3,test1 下创建 test1.html 4,test2 下创建 test2.html 5,配置 ...
- Android 弹性布局 FlexboxLayout了解一下
原文链接:https://mp.weixin.qq.com/s/Mi3cK7xujmEMI_rc51-r4g RelativeLayout.LinearLayout等常用布局相信大家早已耳熟能详,今天 ...
- C# 实现连连看功能
本文是利用C#实现连连看的小例子,以供学习分享使用.如有不足之处,还望指正. 思路: 初始化布局(横竖十行十列,共100个单元格,每一个格一个按钮,背景图为水果图片,随机生成) . 初始化对应棋盘(用 ...
- Android 打包混淆
将项目改成Module //项目build.gradle的applicationId注释掉 修改apply plugin: 'com.android.library' 打包混淆脚本 //在项目的bui ...
- <自动化测试方案书>方案书目录排版
自动化测试方案书 一.介绍 QQ交流群:585499566 这篇是一个系列,用来给需要做自动化测试方案的人做个参考,文章的内容是我收集网上和自己工作经验所得,希望能够给你们有所帮助 背景:因为工作需要 ...
- 堆栈的应用——用JavaScript描述数据结构
栈(stack)又名堆栈,它是一种运算受限的线性表.其限制是仅允许在表的一端进行插入和删除运算.这一端被称为栈顶,相对地,把另一端称为栈底. 一.实现一个栈类Stack 基于堆栈的特性,可以用数组做线 ...
- hadoop,spark,Zookeeper,,, 这些名字都是怎么来的呢?
Apache 首先我们要明白,Apache 是一个 http 服务器,而我们熟悉的另一种说法"Apache Hadoop"中的 Apache 则指的是 Apache 软件基金会.& ...
- Bean named '...' is expected to be of type [...] but was actually of type [com.sun.proxy.$Proxy7解决方法
报错 三月 07, 2017 8:09:52 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepare ...
- python第一百一十一天 --Django 6 model 的相关操作
创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层执行数据库操作 import MySQLdb def GetList(sql): db ...