当前位置:网站首页>POJ 3177 redundant paths POJ 3352 road construction (dual connection)
POJ 3177 redundant paths POJ 3352 road construction (dual connection)
2022-07-07 01:42:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack
POJ 3177 Redundant Paths
POJ 3352 Road Construction
The question : The two questions are the same . A piece of code can hand in . Given a connected undirected graph , How many edges can make a graph become a biconnected graph
Ideas : Find biconnection first . Shrink a little . The calculated penetration is 1 The number of , then ( Number + 1) / 2 That's the answer. ( This problem can be solved because there is only one connected block , Suppose there are more than one , You can't do this )
Code :
#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;}
Copyright notice : This article is the original article of the blogger , Blog , Do not reprint without permission .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116888.html Link to the original text :https://javaforall.cn
边栏推荐
- AcWing 361. 观光奶牛 题解(spfa求正环)
- AcWing 1140. Shortest network (minimum spanning tree)
- 爬虫实战(六):爬笔趣阁小说
- 字符串的相关编程题
- AcWing 345. Cattle station solution (nature and multiplication of Floyd)
- 域分析工具BloodHound的使用说明
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
- LeetCode:1175. 质数排列
- AcWing 344. Solution to the problem of sightseeing tour (Floyd finding the minimum ring of undirected graph)
- C语言【23道】经典面试题【下】
猜你喜欢
Basic introduction and use of dvajs
Set WordPress pseudo static connection (no pagoda)
AI automatically generates annotation documents from code
The difference between Tansig and logsig. Why does BP like to use Tansig
Box stretch and pull (left-right mode)
Dark horse notes - exception handling
Gin introduction practice
AcWing 361. Sightseeing cow problem solution (SPFA seeking positive ring)
云呐-工单管理制度及流程,工单管理规范
Installation of gazebo & connection with ROS
随机推荐
7.6 simulation summary
Appium automation test foundation uiautomatorviewer positioning tool
域分析工具BloodHound的使用说明
前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (上)
Transplant DAC chip mcp4725 to nuc980
Appium自动化测试基础 — uiautomatorviewer定位工具
hdu 4661 Message Passing(木DP&amp;组合数学)
MySQL最基本的SELECT(查询)语句
Hutool post requests to set the body parameter to JSON data
Add the applet "lazycodeloading": "requiredcomponents" in taro,
Gin 入门实战
Right mouse button customization
uva 1401 dp+Trie
1123. The nearest common ancestor of the deepest leaf node
C language instance_ five
Using the entry level of DVA in taro3.*
增加 pdf 标题浮窗
js如何快速创建一个长度为 n 的数组
tansig和logsig的差异,为什么BP喜欢用tansig