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;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)的更多相关文章

  1. POJ 3177 Redundant Paths & POJ 3352 Road Construction(双连通分量)

    Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...

  2. POJ 3177 Redundant Paths POJ 3352 Road Construction

    这两题是一样的,代码完全一样. 就是给了一个连通图,问加多少条边可以变成边双连通. 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树 ...

  3. tarjan算法求桥双连通分量 POJ 3177 Redundant Paths

    POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accept ...

  4. POJ 3177 Redundant Paths(边双连通的构造)

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13717   Accepted: 5824 ...

  5. POJ 3177——Redundant Paths——————【加边形成边双连通图】

    Redundant Paths Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  6. poj 3177 Redundant Paths

    题目链接:http://poj.org/problem?id=3177 边双连通问题,与点双连通还是有区别的!!! 题意是给你一个图(本来是连通的),问你需要加多少边,使任意两点间,都有两条边不重复的 ...

  7. POJ - 3177 Redundant Paths(边双连通分支)(模板)

    1.给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图. 2. 3. //边双连通分支 /* 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话, 把双连通子图 ...

  8. [双连通分量] POJ 3177 Redundant Paths

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13712   Accepted: 5821 ...

  9. poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11047   Accepted: 4725 ...

随机推荐

  1. POJ 4003 Bob’s Race && HDU4123 Bob’s Race (dfs+rmq)

    Bob’s Race Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 378   Accepted: 119 Descript ...

  2. 属性“dataProvider”有多个初始值设定项。(注意:“dataProvider”是“mx.charts.BarChart”的默认属性)。

    1.错误描写叙述 属性"dataProvider"有多个初始值设定项.(注意:"dataProvider"是"mx.charts.BarChart&q ...

  3. 点滴的积累---J2SE学习小结

    点滴的积累---J2SE学习小结 什么是J2SE J2SE就是Java2的标准版,主要用于桌面应用软件的编程:包括那些构成Java语言核心的类.比方:数据库连接.接口定义.输入/输出.网络编程. 学习 ...

  4. hdu2151(递推dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2151 分析: DP.思路:全盘扫描.     i表示时间,l表示第几棵树,方程:     step[i ...

  5. spring web.xml配置服务启动后执行文件

    <bean id="readXmlService" class="com.xxx.xxx.readXmlServiceImpl" init-method= ...

  6. poj3140(树的dfs)

    题目链接:http://poj.org/problem?id=3140 题意:给定一棵n棵节点的树,求删去某条边后两个分支的最小差异值. 分析:num[u]表示以u点为根节点的子树的总人数,那么不在该 ...

  7. 飘逸的python - __new__、__init__、__call__傻傻分不清

    __new__: 对象的创建,是一个静态方法.第一个參数是cls.(想想也是,不可能是self,对象还没创建,哪来的self) __init__ : 对象的初始化, 是一个实例方法,第一个參数是sel ...

  8. VS调试技巧之附加进程

    用过VS一段时间的程序猿们相信都有过这种调试经历:每次按下F5进行断点调试时,都要等待好长时间:先让解决方式编译通过,然后启动VS自带的简版IIS作为server启动,进而开启浏览器,最后进行对应的操 ...

  9. Bitmap

    Bitmap篇   在前一篇中介绍了使用API做Distinct Count,但是计算精确结果的API都较慢,那有没有能更快的优化解决方案呢? 1. Bitmap介绍 <编程珠玑>上是这样 ...

  10. [jQuery]地图浏览:如何实现图片的放大缩小和点击之后的位置居中

    HTML代码: <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis. ...