边双联通问题求解(构造边双连通图)POJ3352(Road Construction)
题目链接:传送门
题目大意:给你一副无向图,问至少加多少条边使图成为边双联通图
题目思路:tarjan算法加缩点,缩点后求出度数为1的叶子节点个数,需要加边数为(leaf+1)/2
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cctype>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <climits>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define fi first
#define se second
#define ping(x,y) ((x-y)*(x-y))
#define mst(x,y) memset(x,y,sizeof(x))
#define mcp(x,y) memcpy(x,y,sizeof(y))
#define Min(x,y) (x<y?x:y)
#define Max(x,y) (x>y?x:y)
using namespace std;
#define gamma 0.5772156649015328606065120
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define N 5005
#define maxn 10005
typedef long long LL;
typedef pair<int,int> PII; vector<int>V[];
int n,m,hcnt,deep;
struct Node{
int to,next;
Node(){}
Node(int a,int b):to(a),next(b){}
}node[N];
int head[N],vis[N],low[N],dfn[N];
int d[N]; inline void add(int x,int y){
node[hcnt]=Node(y,head[x]);
head[x]=hcnt++;
} inline void init(){
mst(d,);
mst(vis,); vis[]=;
mst(head,-);
hcnt=;
deep=;
low[]=dfn[]=;
} void dfs(int x,int father){
for(int i=head[x];~i;i=node[i].next){
int e=node[i].to;
if(e==father)continue;
if(vis[e])low[x]=min(low[x],dfn[e]);
else{
vis[e]=;
low[e]=dfn[e]=++deep;
dfs(e,x);
low[x]=min(low[x],low[e]);
}
}
} int main(){
int i,j,group,Case=,x,y;
while(scanf("%d%d",&n,&m)!=EOF){
for(i=;i<=n;++i)V[i].clear();
init();
while(m--){
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
V[x].push_back(y); ///两点之间联通
V[y].push_back(x); ///在缩点之后用于计算度数
}
dfs(,-); ///tarjan算法
int leaf=;
for(i=;i<=n;++i)
if(V[i].size())
for(j=;j<V[i].size();++j){
int e=V[i][j];
if(low[i]!=low[e])
d[low[i]]++;
}
for(i=;i<=deep;++i)
if(d[i]==)++leaf;
printf("%d\n",(leaf+)/);
}
return ;
}
边双联通问题求解(构造边双连通图)POJ3352(Road Construction)的更多相关文章
- POJ3352 Road Construction (双连通分量)
Road Construction Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ3352 Road Construction(边双连通分量)
...
- POJ-3352 Road Construction,tarjan缩点求边双连通!
Road Construction 本来不想做这个题,下午总结的时候发现自己花了一周的时间学连通图却连什么是边双连通不清楚,于是百度了一下相关内容,原来就是一个点到另一个至少有两条不同的路. 题意:给 ...
- POJ3352 Road Construction 双连通分量+缩点
Road Construction Description It's almost summer time, and that means that it's almost summer constr ...
- POJ3352 Road Construction Tarjan+边双连通
题目链接:http://poj.org/problem?id=3352 题目要求求出无向图中最少需要多少边能够使得该图边双连通. 在图G中,如果任意两个点之间有两条边不重复的路径,称为“边双连通”,去 ...
- poj3352 Road Construction & poj3177 Redundant Paths (边双连通分量)题解
题意:有n个点,m条路,问你最少加几条边,让整个图变成边双连通分量. 思路:缩点后变成一颗树,最少加边 = (度为1的点 + 1)/ 2.3177有重边,如果出现重边,用并查集合并两个端点所在的缩点后 ...
- POJ 2942 Knights of the Round Table 补图+tarjan求点双联通分量+二分图染色+debug
题面还好,就不描述了 重点说题解: 由于仇恨关系不好处理,所以可以搞补图存不仇恨关系, 如果一个桌子上面的人能坐到一起,显然他们满足能构成一个环 所以跑点双联通分量 求点双联通分量我用的是向栈中pus ...
- hdu 4738 (双联通求桥)
2013 ACM/ICPC Asia Regional Hangzhou Online 题目大意:有n个岛,曹操在一些岛之间建了一些桥,每个桥上有一些士兵把守,周瑜只有一个炸弹只能炸掉一个桥,炸弹需要 ...
- poj 3694双联通缩点+LCA
题意:给你一个无向连通图,每次加一条边后,问图中桥的数目. 思路:先将图进行双联通缩点,则缩点后图的边就是桥,然后dfs记录节点深度,给出(u,v)使其节点深度先降到同一等级,然后同时降等级直到汇合到 ...
随机推荐
- [Spring boot] Configuring and Accessing a Data Source
We need our data persistence with configuring our datasouce: In application.properties: spring.h2.co ...
- Excel中如何将时间戳转为时间?
Unix时间戳转换Excel时间? Excel中如何将时间戳转为时间? Excel默认不支持Unix格式时间戳,这在导入数据时十分不便.可以用以下公式将时间戳转换成Excel格式的时间: =(x+8* ...
- [WebView学习之三]:使用WebView来创建Apps
上一篇我们学习了([WebView学习之二]:使用Web Apps 支持不同分辨率屏),今天我们来继续学习. (博客地址:http://blog.csdn.net/developer_jiangqq) ...
- LINPACK測试
1简单介绍 LINPACK是线性系统软件包(Linear system package) 的缩写. Linpack如今在国际上已经成为最流行的用于測试高性能计算机系统浮点性能的benchmark.通过 ...
- (二)Maven 基本概念——依赖、生命周期、仓库管理、聚合&继承
1. 依赖配置 1.1 依赖配置主要包含如下元素: <!--添加依赖配置--> <dependencies> <!--项目要使用到junit的jar包,所以在这里添加ju ...
- STL vector的构造函数和析构函数(2)
原文来自:点击打开链接 译文: public member function vector的构造器:这里我仅仅翻译C++11的,C++98的就不翻译了. 构造器原型: <vector> s ...
- js 判断是否为数组
http://www.jb51.net/article/79939.htm Object.prototype.toString.call([1,2,3,4]) == '[object Array]'
- PHP+Redis 实例 页面缓存
前提分析! 上面的图,我分为了三个层级去做页面缓存,其实不一定要三个层面都实现的,如果你做了页面级的,项目初期是够了,作为接口级,基本可以解决很多吞吐量. 对于上面的三个层级,我用了同一个方法去做. ...
- javascript跨浏览器事件对象类库
一.前言 学习了javascript事件后,个人总结归纳了跨浏览器事件对象类库,方便以后使用,现分享给大家. 二.事件对象封装 将对浏览器事件对象的操作封装成eventObject.js方便调用 // ...
- Python内存管理:垃圾回收
http://blog.csdn.net/pipisorry/article/details/39647931 Python GC主要使用引用计数(reference counting)来跟踪和回收垃 ...