POJ 3352Road Construction 边双联通分量
来源:互联网 发布:淘宝店铺导航怎么删除 编辑:程序博客网 时间:2024/05/29 14:54
题目:http://poj.org/problem?id=3352
题意:加上最少的边,使得改造后的图中去掉任意一条边后图依然连通,题中任意两个点之间不会有重边
思路:删掉任意一条边图依然连通,意味着任意两点间有至少两条通路。对于边双连通分量内的任意两点,至少会有两条通路,所以求边双连通分量,缩点,求出度为1的点数leaf,答案就是(leaf + 1) / 2,原因如下:首先把两个最近公共祖先最远的两个叶节点之间连接一条边,这样可以把这两个点到祖先的路径上所有点收缩到一起,因为一个形成的环一定是双连通的。然后再找两个最近公共祖先最远的两个叶节点,这样一对一对找完,恰好是(leaf+1)/2次,把所有点收缩到了一起。。
总结:双连通分量第一题。不得不说,学双连通分量比强连通分量麻烦多了,并不难,主要是找不到一份好的资料,大神们讲解含糊,姿势各异,,,于是直接找了一道题,一边看讲解,一遍看题实验。。。
主要参考资料有:http://blog.csdn.net/leolin_/article/details/7244094
http://www.cnblogs.com/fornever/archive/2011/09/17/2179448.html
http://www.cnblogs.com/scau20110726/archive/2013/05/18/3085959.html
http://blog.sina.com.cn/s/blog_71aa4dbb01010pq0.html
http://blog.csdn.net/tsaid/article/details/6895908 (代码是用了这位大神的代码,感觉姿势比较优美,符合个人口味,跟tarjan算法求强连通分量差别不大)
在此感谢各位聚聚
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;const int N = 1010;struct edge{ int to, next;}G[N*5];int dfn[N], low[N], st[N], dcc[N], head[N];int index, cnt, num, top;bool vis[N];int n, m;void init(){ memset(head, -1, sizeof head); memset(dfn, -1, sizeof dfn); memset(vis, 0, sizeof vis); index = cnt = num = top = 0;}void add_edge(int v, int u){ G[cnt].to = u; G[cnt].next = head[v]; head[v] = cnt++;}void tarjan(int v, int fa){ dfn[v] = low[v] = index++; vis[v] = true; st[top++] = v; int u, t; for(int i = head[v]; i != -1; i = G[i].next) { u = G[i].to; if(u == fa) continue; /*回访到父亲节点,跳过*/ if(dfn[u] == -1) { tarjan(u, v); low[v] = min(low[v], low[u]); if(dfn[v] < low[u]) /*dfn[v] < low[u],说明边(v,u)就是一个桥,于是弹出一个边连通分量的所有点,v并不属于这个边连通分量,所以弹出到u*/ { num++; do { t = st[--top]; vis[t] = false; dcc[t] = num; }while(t != u); } } else if(vis[u]) low[v] = min(low[v], dfn[u]); }}void slove(){ for(int i = 1; i <= n; i++) if(dfn[i] == -1) tarjan(i, -1); int deg[N]; memset(deg, 0, sizeof deg); for(int i = 1; i <= n; i++) for(int j = head[i]; j != -1; j = G[j].next) if(dcc[i] != dcc[G[j].to]) deg[dcc[i]]++, deg[dcc[G[j].to]]++; int res = 0; for(int i = 0; i <= num; i++) if(deg[i] == 2) res++; printf("%d\n", (res + 1) / 2);}int main (){ int a, b; while(~ scanf("%d%d", &n, &m)) { init(); for(int i = 0; i < m; i++) { scanf("%d%d", &a, &b); /*无向图建两条边*/ add_edge(a, b); add_edge(b, a); } slove(); } return 0;}
- POJ 3352|Road Construction|边双联通分量|Tarjan
- POJ 3352Road Construction 边双联通分量
- 【POJ 3352】 Road Construction(边联通分量入门)
- Road Construction+求双联通分量、割点、桥+POJ
- POJ 3352 Road Construction&& POJ 3177 Redundant Paths 双联通分量
- poj 3352 Road Construction(双联通)
- POJ3352 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
- POJ 3352 Road Construction
- poj 3352 Road Construction
- POJ 3352 Road Construction
- POJ 3352 Road Construction
- poj 3352 Road Construction
- java运行jar包中指定Class的main方法
- 对于sprintf(s,"%03d",n)
- HDU 1074 Doing Homework(状压DP+储存路径)
- Scala之Option
- DexClassLoader4.4.2动态加载分析(磁盘加载分析)
- POJ 3352Road Construction 边双联通分量
- 基于JBox2D物理引擎开发的“雷电”小游戏(三)——模拟并显示世界
- java之List接口
- java静态分配和动态分配
- 内核中的内存申请:kmalloc、vmalloc、kzalloc、get_free_pages 之间的区别
- Apache+jk+tomcat负载均衡详细配置方法
- 数据挖掘-关联规则
- HDOJ(HDU) 2106 decimal system(进制相互转换问题)
- SM2算法第五篇:socket的基本原理与实现