题目大意:一个图,要求你加入最少的边,使得最后得到的图为一个边双连通分支。所谓的边双连通分支,即不存在桥的连通分支(题目保证数据中任意两点都联通)。

解题思路:先用tarjan算法进行缩点建立DAG图, 然后再进行寻找度为1的点有个数x, 那么需要添加的边即为(x+1)/ 2;

起初这样写, 一直WA,然后发现下面两个数据,发现并不能过。

#include <stdio.h>
#include <set>
#include <vector>
#include <string.h>
#include <algorithm>
using namespace std; const int N = ;
vector<int>G[N];
vector<pair<int, int> >DAG;
int dfn[N], low[N], mk[N];
int tot;
int n, m; void init()
{
tot = ;
DAG.clear();
for(int i=; i<=n; ++ i)
{
mk[i] = ;
G[i].clear();
dfn[i] = low[i] = -;
}
} void tarjan(int u, int f)
{
dfn[u] = low[u] = ++ tot;
for(int i = ; i < G[u].size(); ++ i)
{
int v = G[u][i];
if(dfn[v] == -)
{
tarjan(v, u);
low[u] = min(low[u], low[v]);
if(dfn[u] < low[v])
DAG.push_back(make_pair(low[u], low[v]));
}
else if(v != f)
low[u] = min(low[u], dfn[v]);
}
} void solve()
{
init();
for(int i=; i<=m; ++ i)
{
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
tarjan(, -);
for(int i=; i<DAG.size(); ++ i)
{
pair<int, int> S = DAG[i];
mk[S.first] ++, mk[S.second] ++;
}
int ans = ;
for(int i=; i<=n; ++ i)
{
if(mk[i] == )
ans ++;
}
printf("%d\n", (ans + ) / );
} int main()
{
while(scanf("%d%d", &n, &m) != EOF)
solve();
return ;
}

需要特别注意两组数据:

2 2

1 2

1 2

2 1

1 2

答案分别是:

0

1

代码如下:

#include <stdio.h>
#include <set>
#include <vector>
#include <string.h>
#include <algorithm>
using namespace std; const int N = ;
vector<int>G[N];
int dfn[N], low[N], mk[N];
int tot;
int n, m; void init()
{
tot = ;
for(int i=; i<=n; ++ i)
{
mk[i] = ;
G[i].clear();
dfn[i] = low[i] = -;
}
} void tarjan(int u, int f)
{
dfn[u] = low[u] = ++ tot;
for(int i = ; i < G[u].size(); ++ i)
{
int v = G[u][i];
if(dfn[v] == -)
{
tarjan(v, u);
low[u] = min(low[u], low[v]);
}
else if(v != f)
low[u] = min(low[u], dfn[v]);
}
} void solve()
{
init();
for(int i=; i<=m; ++ i)
{
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
tarjan(, -);
for(int i = ; i <= n; ++ i)
{
for(int j = ; j < G[i].size(); ++ j)
{
if(low[i] != low[G[i][j]])
mk[low[i]] ++;
}
}
int ans = ;
for(int i = ; i <= n; ++ i)
if(mk[i] == )
ans ++;
printf("%d\n", (ans + ) / );
} int main()
{
while(scanf("%d%d", &n, &m) != EOF)
solve();
return ;
}

POJ 3352-Road Construction (图论-双边联通分支算法)的更多相关文章

  1. POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)

    POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...

  2. poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】

    Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10141   Accepted: 503 ...

  3. Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)

     http://blog.csdn.net/geniusluzh/article/details/6619575 在说Tarjan算法解决桥和边双连通分量问题之前我们先来回顾一下Tarjan算法是如何 ...

  4. POJ 3352 Road Construction 双联通分量 难度:1

    http://poj.org/problem?id=3352 有重边的话重边就不被包含在双连通里了 割点不一定连着割边,因为这个图不一定是点连通,所以可能出现反而多增加了双连通分量数的可能 必须要用割 ...

  5. POJ 3352 Road Construction ; POJ 3177 Redundant Paths (双联通)

    这两题好像是一样的,就是3177要去掉重边. 但是为什么要去重边呢??????我认为如果有重边的话,应该也要考虑在内才是. 这两题我用了求割边,在去掉割边,用DFS缩点. 有大神说用Tarjan,不过 ...

  6. 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 ...

  7. poj 3352 Road Construction(边双连通分量+缩点)

    题目链接:http://poj.org/problem?id=3352 这题和poj 3177 一样,参考http://www.cnblogs.com/frog112111/p/3367039.htm ...

  8. POJ 3352 Road Construction(边—双连通分量)

    http://poj.org/problem?id=3352 题意: 给出一个图,求最少要加多少条边,能把该图变成边—双连通. 思路:双连通分量是没有桥的,dfs一遍,计算出每个结点的low值,如果相 ...

  9. 【边双连通】poj 3352 Road Construction

    http://poj.org/problem?id=3352 [题意] 给定一个连通的无向图,求最少加多少条边使得这个图变成边双连通图 [AC] //#include<bits/stdc++.h ...

随机推荐

  1. mysql优化思路

    /* mysql优化思路     1.数据库设计     2.sql语句优化     3.数据库参数设置     4.恰当的硬件资源和操作系统        数据库设计         数据的3NF( ...

  2. yII中利用urlManager将URL改写成restful风格 这里主要涉及url显示样式

    1.打开config文件夹下面的mian.php   2.修改内容   如把地址http://www.test.com/index.php?r=site/page/sid/1修改为http://www ...

  3. Silverlight管理系统源代码(SilverlightOAFlame开发框架主要提供二次开发)

    Silverlight OA系统简介 系统功能简介 l 程序界面介绍: 左侧为主菜单,主菜单可以展开和收起,主菜单下面的所有模块都可以在数据库中扩展增加,模块的权限和用户角色挂钩,可以在数据库中创建多 ...

  4. python---Memcached

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...

  5. 千里积于跬步——流,向量场,和微分方程[转载]

    在很多不同的科学领域里面,对于运动或者变化的描述和建模,都具有非常根本性的地位--我个人认为,在计算机视觉里面,这也是非常重要的. 什么是"流"? 在我接触过的各种数学体系中,对于 ...

  6. Java中的枚举

    枚举通过enum关键字进行定义.实际上当定义一个枚举类型的时候,该类型默认继承了Enum类. 枚举的定义格式如下: enum Color { RED,BLUE,GREEN; } 下面演示一个枚举变量的 ...

  7. ubuntu14.04配置impala的odbc连接

    cdh hive和impala的odbc驱动 http://www.cloudera.com/downloads.html.html 选择 SLES 11 64-bit下载: http://www.c ...

  8. 【摘】top命令

    1.重要参数解释 VIRT:virtual memory usage.Virtual这个词很神,一般解释是:virtual adj.虚的, 实质的, [物]有效的, 事实上的.到底是虚的还是实的?让G ...

  9. 使用Maven编译项目时提示程序包javax.servlet.http不存在

    将apache-tomcat-8.0.23\lib下的servlet-api.jar拷贝到C:\Program Files\Java\jdk1.8.0_31\jre\lib\ext下即可

  10. js实现图片的淡入淡出

    思想: 其实是运动的一种,就是当鼠标移入div中时,将div的透明度变大, 当鼠标移动出来的时候透明度变回原来. 你可以尝试写一下,不会再看看代码 <style> #div1{ width ...