POJ 3352-Road Construction (图论-双边联通分支算法)
题目大意:一个图,要求你加入最少的边,使得最后得到的图为一个边双连通分支。所谓的边双连通分支,即不存在桥的连通分支(题目保证数据中任意两点都联通)。
解题思路:先用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 (图论-双边联通分支算法)的更多相关文章
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...
- poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】
Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10141 Accepted: 503 ...
- Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)
http://blog.csdn.net/geniusluzh/article/details/6619575 在说Tarjan算法解决桥和边双连通分量问题之前我们先来回顾一下Tarjan算法是如何 ...
- POJ 3352 Road Construction 双联通分量 难度:1
http://poj.org/problem?id=3352 有重边的话重边就不被包含在双连通里了 割点不一定连着割边,因为这个图不一定是点连通,所以可能出现反而多增加了双连通分量数的可能 必须要用割 ...
- POJ 3352 Road Construction ; POJ 3177 Redundant Paths (双联通)
这两题好像是一样的,就是3177要去掉重边. 但是为什么要去重边呢??????我认为如果有重边的话,应该也要考虑在内才是. 这两题我用了求割边,在去掉割边,用DFS缩点. 有大神说用Tarjan,不过 ...
- 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 ...
- poj 3352 Road Construction(边双连通分量+缩点)
题目链接:http://poj.org/problem?id=3352 这题和poj 3177 一样,参考http://www.cnblogs.com/frog112111/p/3367039.htm ...
- POJ 3352 Road Construction(边—双连通分量)
http://poj.org/problem?id=3352 题意: 给出一个图,求最少要加多少条边,能把该图变成边—双连通. 思路:双连通分量是没有桥的,dfs一遍,计算出每个结点的low值,如果相 ...
- 【边双连通】poj 3352 Road Construction
http://poj.org/problem?id=3352 [题意] 给定一个连通的无向图,求最少加多少条边使得这个图变成边双连通图 [AC] //#include<bits/stdc++.h ...
随机推荐
- android图像与图像处理系列(一、Bitmap和BitmapFactory)
1.Drawable对象 Android应用添加了Drawabe资源之后,Android SDK会为这份资源文件在R清单文件中创建一个索引项:R.drawable.file_name,接着我们可以在x ...
- 基于Python的接口测试框架
分析 接口是基于HTTP协议的,那么说白了,就是发起HTTP请求就行了,对于Python来说比较简单.直接使用requests就可以很轻松的完成任务. 架构 整个框架是比较小的,涉及的东西也比较少,只 ...
- MapReduce格式与类型
MapReduce Types MapReduce是一个简单的数据处理模型,map与reduce的输入和输出类型都为key-value形式的键值对. map: (K1, V1) → list(K2, ...
- OAF_JDBC系列2 - 通过JDBC连接SQLSERVER数据库DriverManager.getConnection
d try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); St ...
- ADF_Starting系列7_使用EJB/JPA/JSF通过ADF构建Web应用程序之创建UI View
2013-05-01 Created By BaoXinjian
- css绝对定位问题
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 使用gson解析,生成Json
包:gson-2.3.jarJson文本解析为Java对象:Java对象生成为Json文本 import com.google.gson.Gson; public class TestGson { c ...
- 在silverlight中通过WCF连接ORACLE DB数据库(转)
转自 http://hi.baidu.com/qianlihanse/item/458aa7c8d93d4e0cac092ff4 这不是我的原创,我也是上网学习的~ How to get data f ...
- Android自学笔记:环境配置
Info: 自学Android之旅第二篇,初步学习会有疏漏,以后我会不断修改补全,直到完美. 2014-10-09:初版 2014-11-12: 重新配置了一台电脑,更新在学习robotium过程中, ...
- MMU
1.MMU功能 将虚拟地址转化为物理地址:访问权限管理. 2.地址转化 2.1 总体分析 2.2 一级转换格式 位解释:(段设置) B:表示是否使能write buffer: C: 表示是否开启ca ...