POJ 3352 Road Construction / 边双连通分量
来源:互联网 发布:重度抑郁症的表现知乎 编辑:程序博客网 时间:2024/05/22 03:19
给你一张图 求最少加几条边可以使全图双连通
首先缩点 然后求有几个叶子结点 设有n个 答案是(n+1)/ 2
如果n=1那么需要1条(和根相连)如果n=2 需要2条这两个叶子节点相连 如果n=3 需要2条(其中2条相互连接 在求双连通 。。。。)
这题主要是学习求边双连通分量的算法有些蛋疼
1.应该是无向图 网上有很多人用了求有向图强连通分量的tarjan算法 特地对比了一下
2.有些人说直接用low判断是否是一个边连通分量 有人却说不可以 彻底让我崩溃
3.书上树求双连通分量先求出割边 在做一次dfs 不经过前面标记的割边 找出所有的边双连通分量
特地都写了一下 还不太懂
用强连通分量的tarjan 算法
其中由于是无向图 缩点后重新求度数的时候u-v 和v-u都+1了 最后判断叶子节点是否等于2
#include <cstdio>#include <cstring>#include <vector>#include <stack>#include <algorithm>using namespace std;const int maxn = 10010;vector <int> G[maxn];int pre[maxn];int low[maxn];int sccno[maxn];int dfs_clock;int scc_cnt;stack <int> S;int n, m;int degree[maxn];int Topo[maxn][maxn];void dfs(int u, int fa){pre[u] = low[u] = ++dfs_clock;S.push(u);for(int i = 0; i < G[u].size(); i++){int v = G[u][i];if(v == fa)continue;if(!pre[v]){dfs(v, u);low[u] = min(low[u], low[v]);if(low[v] > pre[u]){scc_cnt++;while(1){int x = S.top();S.pop();sccno[x] = scc_cnt;if(x == v)break;}}}else if(!sccno[v])low[u] = min(low[u], pre[v]);}}void find_scc(){dfs_clock = scc_cnt = 0;memset(sccno, 0, sizeof(sccno));memset(pre, 0, sizeof(pre));for(int i = 1; i <= n; i++)if(!pre[i])dfs(i, -1);}int main(){while(scanf("%d %d", &n, &m) != EOF){for(int i = 1; i <= n; i++)G[i].clear();while(m--){int u, v;scanf("%d %d", &u, &v);G[u].push_back(v);G[v].push_back(u);}find_scc();memset(degree, 0, sizeof(degree));for(int i = 1; i <= n; i++){for(int j = 0; j < G[i].size(); j++){int v = G[i][j];if(sccno[i] != sccno[v]){degree[sccno[i]]++;degree[sccno[v]]++;}}}int ans = 0;for(int i = 1; i <= scc_cnt; i++)if(degree[i] == 2)ans++;printf("%d\n", (ans + 1) / 2);}return 0;}
0 0
- [双连通分量]poj 3352 Road Construction
- poj 3352--Road Construction(双连通分量)
- POJ---3352-Road Construction(双连通分量)
- POJ 3352 Road Construction / 边双连通分量
- POJ 3352 Road Construction(边双连通分量)
- POJ 3352 Road Construction (边双连通分量)
- poj 3352 Road Construction 边双连通分量
- POJ 3352 Road Construction(边双连通分量)
- POJ - 3352 Road Construction(边双连通分量)
- POJ-3352 Road Construction (边双连通分量[Tarjan])
- poj 3352 Road Construction(边-双连通分量)
- poj 3352 Road Construction(边双连通分量)
- POJ-3352 Road Construction(边双连通分量+缩点)
- POJ 3352 Road Construction (边双连通分量)
- POJ 3352 Road Construction(边双连通分量)
- poj 3352 Road Construction(Tarjan,边双连通分量)
- poj 3352 Road Construction 双连通分量+缩点 模板
- poj3352 Road Construction(边双连通分量)
- 《软件架构师应该知道的97件事》 读书笔记
- MFC学习(一)——对话框和Windows标准控件
- Around the World in Eighty Days 19/201
- 设计模式(23)——访问者模式与双分派
- poj 2709 Painter
- POJ 3352 Road Construction / 边双连通分量
- HDU Eddy's digital Roots
- 《深度探索C++对象模型》--2 构造函数语意学
- 设计模式23种小结
- Linux学习路线之linux的amr开发学习方法
- maven学习笔记【配置参考】
- 高斯消元法
- 5.3 与其他Fragment交互
- 【VS2010学习笔记】【函数学习】二(SetTimer()函数)