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 ...
随机推荐
- MapReduce 2简介
在Hadoop 1.0版本中,mapred.job.tracker决定了执行MapReduce程序的方式,若设置为local,则使用本地的作业运行器,若设置为主机:端口(eb179:9001),则该配 ...
- linux shell mysql 数据库主从同步状态检查告警
需求: 1.监测数据库主从状态 2.获取数据库主要参数 3.可读取配置文件 4.部署位置自适应. 参考资料: http://blog.csdn.net/yf210yf/article/detail ...
- [solr] - 索引数据删除
删除solr索引数据,使用XML有两种写法: 1) <delete><id>1</id></delete> <commit/> 2) < ...
- CSS3 旋转代码备忘
.Aclose { -webkit-transition-property: all; -webkit-transition-duration: .3s; -moz-transition-proper ...
- API接口验证
一.前言 权限验证在开发中是经常遇到的,通常也是封装好的模块,如果我们是使用者,通常指需要一个标记特性或者配置一下就可以完成,但实际里面还是有许多东西值得我们去探究.有时候我们也会用一些开源的权限验证 ...
- BeanFactory 和ApplicationContext(Bean工厂和应用上下文)
Bean工厂(BeanFactory接口),提供了基础的依赖注入支持. 应用上下文(ApplicationContext接口),建立在Bean工厂基础之上,提供了系统架构服务. Application ...
- ASP.NET中进行消息处理(MSMQ) 三(转)
在本文的前两篇文章里对MSMQ的相关知识点进行了介绍,很多阅读过这前两篇文章的朋友都曾问到过这样一些问题: 1.如何把MSMQ应用到实际的项目中去呢? 2.可不可以介绍一个实际的应用实例? 3. ...
- 如何在LLDB下排查message sent to deallocated instance问题
转:http://www.devdiv.com/home.php?mod=space&uid=50901&do=blog&id=50856 在XCode的以前版本中,如果遇到了 ...
- delphi控件属性和事件
常用[属性] Action:该属性是与组件关联的行为,允许应用程序集中响应用户命令 Anchors:与组件连接的窗体的位置点 Align:确定组件的对齐方式 AutoSize:确定组件是否自动 ...
- sql语句Group By用法-转载
sql语句Group By用法一则 2007-10-25 12:00 sql语句Group By用法一则 如果我们的需求变成是要算出每一间店 (store_name) 的营业额 (sales),那怎么 ...