边双联通问题求解(构造边双连通图)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)使其节点深度先降到同一等级,然后同时降等级直到汇合到 ...
随机推荐
- 除了cPickle,cjson外还有没有更高效点的序列化库了
除了cPickle,cjson外还有没有更高效点的序列化库了 http://blog.csdn.net/chen_lovelotus/article/details/7228745 msgpack最快 ...
- 2015 Multi-University Training Contest 1记录
1001 OO's Sequence 分析: 对于例子,能够得到,我们要求的是(1,1)(1,2)(1,3)(1,4)(1,5)(2,2)(2,3)(2,4)(2,5)(3,3)(3,4)(3,5)( ...
- 使用 原生js 制作插件 (javaScript音乐播放器)
1.引用页面 index.html <!DOCTYPE html> <html lang="en"> <head> <meta chars ...
- 5.触摸touch,单点触摸,多点触摸,触摸优先和触摸事件的吞噬
1 触摸 Coco2dx默认仅仅有CCLayer及其派生类才有触摸的功能. 2 单点触摸 打开触摸开关和触摸方式 setTouchEnabled(true); setTouchMode(kCCT ...
- (三)Maven基本概念——常用插件的配置
看注释———— pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http ...
- Hibernate中所有包作用详细讲解
http://liyanblog.cn/articles/2012/09/17/1347848802900.html Hibernate一共包括了23个jar包,令人眼花缭乱.本文将详细讲解Hiber ...
- TCP通过哪些措施,保证传输可靠
TCP是通过什么方式来提供可靠传输的 (合理截断数据包,超时重发,校验,失序重新排序,能够丢弃重复数据,TCP可以进行流量控制) TCP提供一种面向连接的.可靠的字节流服务. 面向连接:意味着两个使 ...
- webpack 与 热编译webpack-dev-server
webpack.config.js 只需要注意加大加粗的地方. var webpack = require("webpack"); var HtmlWebpackPlugin = ...
- AFNet3.0上传图片
很多iOS应用都会遇到需要上传图片的情况,比如修改个人资料时需要上传自己的头像,最近我也遇到了这种情况,首先参考之前写过的AFNetWorking3.0处理请求头和请求内容. 我们在上述博客的基础之上 ...
- CF 482A(Diverse Permutation-相邻距离不同数为k的1~n全排列构造)
A. Diverse Permutation time limit per test 1 second memory limit per test 256 megabytes input standa ...