POJ 3177 Redundant Paths / 边双连通分量
来源:互联网 发布:软件安装调试方案 编辑:程序博客网 时间:2024/05/17 01:58
和上一题一样 给你一张图 求最少加几条边可以使全图双连通 多了重边
多加了一个bool的数组 判断重边 重边只算一条
还是用了有向图的强连通分量 与有向图相比 多了 1.参数fa 父节点 2.如果子节点是其父节点 continue
#include <cstdio>#include <cstring>#include <vector>#include <stack>#include <algorithm>using namespace std;const int maxn = 5010;vector <int> G[maxn];bool ok[maxn][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]);}elselow[u] = min(low[u], pre[v]);}if(low[u] == pre[u]){scc_cnt++;while(1){int x = S.top();S.pop();sccno[x] = scc_cnt;if(x == u)break;}}}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();memset(ok, false, sizeof(ok));while(m--){int u, v;scanf("%d %d", &u, &v);if(!ok[u][v]){G[u].push_back(v);G[v].push_back(u);ok[u][v] = true;}}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 3177 Redundant Paths(双连通分量)
- POJ 3177 Redundant Paths 边的双连通分量
- [边双连通分量] poj 3177 Redundant Paths
- POJ 3177 Redundant Paths(边双连通分量,3级)
- POJ 3177 Redundant Paths / 边双连通分量
- POJ 3177 Redundant Paths(边双连通分量+缩点)
- poj 3177 Redundant Paths 边双连通分量+缩点
- poj 3177 Redundant Paths 边双连通分量
- POJ 3177 Redundant Paths 边双连通分量+缩点
- POJ 3177 Redundant Paths(边双连通分量)
- poj 3177 Redundant Paths 边双连通分量
- POJ-3177 Redundant Paths (边双连通分量[Tarjan])
- POJ 3177Redundant Paths 边双连通分量
- POJ 3177 Redundant Paths(边双连通分量+缩点)
- POJ 3177 Redundant Paths <边双连通分量>
- POJ 3177 Redundant Paths(边双连通分量+缩点)
- POJ 3177 Redundant Paths(边双连通分量+缩点)
- poj 3177 Redundant Paths(Tarjan,边双连通分量)
- linux高级网络配置
- VS2013安装
- 黑马程序员--基础加强--第四篇--继承Thread类和实现Runnable接口有什么区别?
- Python Unittest源码分析
- PC机交叉编译ARM boost库
- POJ 3177 Redundant Paths / 边双连通分量
- 微信服务号VS订阅号
- SurfaceView实现双缓冲避免游戏闪屏
- C++中的const
- Deployment failure on Tomcat 6.x. Could not copy all resources...
- 程序猿起步篇
- 修改AXD编辑状态下字体大小方法
- hibernate-02-17
- 九度OJ 1408 吃豆机器人 -- 动态规划