当前位置:网站首页>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
边栏推荐
猜你喜欢

身体质量指数程序,入门写死的小程序项目

UI control telerik UI for WinForms new theme - vs2022 heuristic theme

黑马笔记---异常处理

Boot - Prometheus push gateway use

Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman

力扣1037. 有效的回旋镖

405 method not allowed appears when the third party jumps to the website

How to manage distributed teams?

boot - prometheus-push gateway 使用

Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
随机推荐
2022 Google CTF segfault Labyrinth WP
Neon Optimization: performance optimization FAQ QA
Let's see through the network i/o model from beginning to end
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
子网划分、构造超网 典型题
树莓派/arm设备上安装火狐Firefox浏览器
搭建【Redis in CentOS7.x】
Js逆向——捅了【马蜂窝】的ob混淆与加速乐
C language - array
Failed to successfully launch or connect to a child MSBuild. exe process. Verify that the MSBuild. exe
【芯片方案设计】脉搏血氧仪
[JS] obtain the N days before and after the current time or the n months before and after the current time (hour, minute, second, year, month, day)
7.6模拟赛总结
Dynamic planning idea "from getting started to giving up"
Make Jar, Not War
JTAG principle of arm bare board debugging
ARM裸板调试之JTAG原理
C # method of calculating lunar calendar date 2022
系统休眠文件可以删除吗 系统休眠文件怎么删除
Meet in the middle