当前位置:网站首页>POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
2022-07-06 17:56:00 【全栈程序员站长】
大家好,又见面了,我是全栈君
POJ 3177 Redundant Paths
POJ 3352 Road Construction
题意:两题一样的。一份代码能交。给定一个连通无向图,问加几条边能使得图变成一个双连通图
思路:先求双连通。缩点后。计算入度为1的个数,然后(个数 + 1) / 2 就是答案(这题因为是仅仅有一个连通块所以能够这么搞,假设有多个,就不能这样搞了)
代码:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 1005;const int M = 20005;int n, m;struct Edge { int u, v, id; bool iscut; Edge() {} Edge(int u, int v, int id) { this->u = u; this->v = v; this->id = id; this->iscut = false; }} edge[M];int first[N], next[M], en;void add_edge(int u, int v, int id) { edge[en] = Edge(u, v, id); next[en] = first[u]; first[u] = en++;}void init() { en = 0; memset(first, -1, sizeof(first));}int pre[N], dfn[N], dfs_clock, bccno[N], bccn;void dfs_cut(int u, int id) { pre[u] = dfn[u] = ++dfs_clock; for (int i = first[u]; i + 1; i = next[i]) { if (edge[i].id == id) continue; int v = edge[i].v; if (!pre[v]) { dfs_cut(v, edge[i].id); dfn[u] = min(dfn[u], dfn[v]); if (dfn[v] > pre[u]) edge[i].iscut = edge[i^1].iscut = true; } else dfn[u] = min(dfn[u], pre[v]); }}void find_cut() { dfs_clock = 0; memset(pre, 0, sizeof(pre)); for (int i = 1; i <= n; i++) if (!pre[i]) dfs_cut(i, -1);}void dfs_bcc(int u) { bccno[u] = bccn; for (int i = first[u]; i + 1; i = next[i]) { if (edge[i].iscut) continue; int v = edge[i].v; if (bccno[v]) continue; dfs_bcc(v); }}void find_bcc() { bccn = 0; memset(bccno, 0, sizeof(bccno)); for (int i = 1; i <= n; i++) { if (!bccno[i]) { bccn++; dfs_bcc(i); } }}int du[N];int main() { while (~scanf("%d%d", &n, &m)) { int u, v; init(); for (int i = 0; i < m; i++) { scanf("%d%d", &u, &v); add_edge(u, v, i); add_edge(v, u, i); } find_cut(); find_bcc(); memset(du, 0, sizeof(du)); for (int i = 0; i < en; i += 2) { if (!edge[i].iscut) continue; int u = bccno[edge[i].u], v = bccno[edge[i].v]; if (u == v) continue; du[u]++; du[v]++; } int cnt = 0; for (int i = 1; i <= bccn; i++) if (du[i] == 1) cnt++; printf("%d\n", (cnt + 1) / 2); } return 0;}
版权声明:本文博主原创文章,博客,未经同意不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116888.html原文链接:https://javaforall.cn
边栏推荐
- 系统休眠文件可以删除吗 系统休眠文件怎么删除
- THREE.AxesHelper is not a constructor
- NEON优化:性能优化常见问题QA
- C language instance_ two
- Taro中添加小程序 “lazyCodeLoading“: “requiredComponents“,
- Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
- golang中的atomic,以及CAS操作
- 【芯片方案设计】脉搏血氧仪
- 736. Lisp 语法解析 : DFS 模拟题
- 力扣1037. 有效的回旋镖
猜你喜欢
【信号与系统】
移植DAC芯片MCP4725驱动到NUC980
Do you understand this patch of the interface control devaxpress WinForms skin editor?
【案例分享】网络环路检测基本功能配置
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]
Installation of gazebo & connection with ROS
Gazebo的安装&与ROS的连接
HMM 笔记
[Niuke] b-complete square
系统休眠文件可以删除吗 系统休眠文件怎么删除
随机推荐
Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
树莓派/arm设备上安装火狐Firefox浏览器
golang中的atomic,以及CAS操作
云呐-工单管理制度及流程,工单管理规范
Yunna - work order management system and process, work order management specification
C语言实例_2
golang中的Mutex原理解析
7.6 simulation summary
C language instance_ five
Realize incremental data synchronization between MySQL and ES
Gnet: notes on the use of a lightweight and high-performance go network framework
The MySQL database in Alibaba cloud was attacked, and finally the data was found
前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
Wood extraction in Halcon
Oracle: Practice of CDB restricting PDB resources
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
The difference between spin and sleep
【C语言进阶篇】指针的8道笔试题
Yunna | work order management software, work order management software app
Do you understand this patch of the interface control devaxpress WinForms skin editor?