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. freemarker自己定义标签(二)

    freemarker自己定义标签 1.自己定义标签 通过自己定义标签,写一个反复指定字符串 2.实现源代码 <html> <head> <meta http-equiv= ...

  2. [android]APP启动界面——SplashActivity

    概念 当前应用程序在启动的时候都会有一个展示自己公司LOGO和APP名字的界面.这个界面成为SplashActivity. 布局 <? xml version="1.0" e ...

  3. Wix学习整理(5)——安装时填写注册表

    原文:Wix学习整理(5)--安装时填写注册表 一 Microsoft操作系统的注册表 什么是注册表? 注册表是Mircrosoft Windows中的一个重要的数据库,用于存储系统和应用程序的设置信 ...

  4. HTTPS原理(转)

    HTTPS是什么 HTTPS全称为Hypertext Transfer Protocol over Secure Socket Layer,及以安全为目标的HTTP通道,简单说就是HTTP的安全版本. ...

  5. bzoj 3519: [Zjoi2014] 消棋子 题解

    [序言]在大家怀疑的眼光下,我做了一个中午和半个下午.调了一个晚上的题目总算A了! [原题] 消棋子是一个有趣的游戏.游戏在一个r * c的棋盘上进行.棋盘的每一个格 子.要么是空,要么是一种颜色的棋 ...

  6. CH BR4思考熊(恒等有理式-逆波兰表达式求值)

    恒等有理式 总时限 10s 内存限制 256MB 出题人 fotile96 提交情况 4/43 描述 给定两个有理式f(X)与g(X),判断他们是否恒等(任意A,如果f(A)与g(A)均有定义,那么f ...

  7. selenium通过WebDriverWait实现ajax测试

    selenium通过WebDriverWait实现ajax测试 AndroidDriver driver = new AndroidDriver(); driver.get("http:// ...

  8. C# 使用Tuple传递多个参数

    Tuple是基于.NET Framework 4.0 及以上版本才有的.微软称它为元组,如果有三个参数那就是三元组.如 Tuple(T1, T2, T3) Tuple的命名空间在 System 很短吧 ...

  9. HttpClient 4.3教程(转载)

    HttpClient 4.3教程(转载) 转自:http://www.yeetrack.com/?p=779 前言 Http协议应该是互联网中最重要的协议.持续增长的web服务.可联网的家用电器等都在 ...

  10. 关于Velocity加减法等四则运算的迷思

    曾今有一个FreeMarker摆在我面前. 我没有好好珍惜, 遇到了Velocity我才想起失去的美好... 需求是把PC网页点击. 手机网页点击.App点击相加得到总点击量显示出来: $articl ...