题目链接:传送门

题目大意:给你一副无向图,问至少加多少条边使图成为边双联通图

题目思路: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)的更多相关文章

  1. POJ3352 Road Construction (双连通分量)

    Road Construction Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u Sub ...

  2. POJ3352 Road Construction(边双连通分量)

                                                                                                         ...

  3. POJ-3352 Road Construction,tarjan缩点求边双连通!

    Road Construction 本来不想做这个题,下午总结的时候发现自己花了一周的时间学连通图却连什么是边双连通不清楚,于是百度了一下相关内容,原来就是一个点到另一个至少有两条不同的路. 题意:给 ...

  4. POJ3352 Road Construction 双连通分量+缩点

    Road Construction Description It's almost summer time, and that means that it's almost summer constr ...

  5. POJ3352 Road Construction Tarjan+边双连通

    题目链接:http://poj.org/problem?id=3352 题目要求求出无向图中最少需要多少边能够使得该图边双连通. 在图G中,如果任意两个点之间有两条边不重复的路径,称为“边双连通”,去 ...

  6. poj3352 Road Construction & poj3177 Redundant Paths (边双连通分量)题解

    题意:有n个点,m条路,问你最少加几条边,让整个图变成边双连通分量. 思路:缩点后变成一颗树,最少加边 = (度为1的点 + 1)/ 2.3177有重边,如果出现重边,用并查集合并两个端点所在的缩点后 ...

  7. POJ 2942 Knights of the Round Table 补图+tarjan求点双联通分量+二分图染色+debug

    题面还好,就不描述了 重点说题解: 由于仇恨关系不好处理,所以可以搞补图存不仇恨关系, 如果一个桌子上面的人能坐到一起,显然他们满足能构成一个环 所以跑点双联通分量 求点双联通分量我用的是向栈中pus ...

  8. hdu 4738 (双联通求桥)

    2013 ACM/ICPC Asia Regional Hangzhou Online 题目大意:有n个岛,曹操在一些岛之间建了一些桥,每个桥上有一些士兵把守,周瑜只有一个炸弹只能炸掉一个桥,炸弹需要 ...

  9. poj 3694双联通缩点+LCA

    题意:给你一个无向连通图,每次加一条边后,问图中桥的数目. 思路:先将图进行双联通缩点,则缩点后图的边就是桥,然后dfs记录节点深度,给出(u,v)使其节点深度先降到同一等级,然后同时降等级直到汇合到 ...

随机推荐

  1. DevExpress.Build

    using System.Collections.Generic; using Microsoft.Build.AppxPackage; using Microsoft.Build.Framework ...

  2. RGB颜色工具大全 and 网页配色方案

    RGB颜色工具:http://www.ostools.net/commons?type=3 配色方案:http://www.wzsky.net/html/Website/Color/103380.ht ...

  3. hibernate hql 语句中 in 的用法

    例子描述查询一些班级中的学生 /** * * @param city * @return */ public List<Student> studentList(final Integer ...

  4. python——unpack问题 ocr_recognize timeout , exception:unpack requires a string argument of length 46

    rObjBuff = "".join(unpack('=%ds' % ObjLen, recv_buf[6+i*ObjLen:6+(i+1)*ObjLen]))score, bbo ...

  5. (转)C的代码是如何变成程序的

    原文链接:http://blog.csdn.net/fz_ywj/article/details/8769825 C语言是一门典型的编译语言,源代码文件需要编译成目标代码文件才能运行.可以认为程序文件 ...

  6. Python 实现的猫脸识别、人脸识别器。

    代码地址如下:http://www.demodashi.com/demo/13071.html 前言: OpenCV是开源的跨平台计算机视觉库,提供了Python等语言的接口,实现了图像处理和计算机视 ...

  7. Refactoring之——代码的坏味道(一)过长方法

    1 代码的坏味道 重构一书中提到了22种代码的坏味道,大致可以分为几类. 识别代码的坏味道,有助于发现代码的潜在问题,从而可以有的放矢的修改现有代码,使之不断完善. 1.1 Bloaters(臭鲱,暂 ...

  8. asp.net 复习总结

    1.asp.net页面上格式化时间是:<%# Eval("jsBianhao", "{0:yyyy/MM/dd}")%>

  9. 出现蓝屏代码0x0000007b的原因及解决办法

    出现蓝屏代码0x0000007b的原因通常是硬盘的存储控制器驱动加载错误,我们可以通过对BIOS界面进行修复来解决这个问题.下面小编将详细介绍解决蓝屏代码0x0000007b的方法,一起来看看吧 导致 ...

  10. js遮罩层弹出显示效果组件化

    1.在web开发中经常遇到遮罩层的效果,可以将这种常用方法通用化 function showid(idname){ var isIE = (document.all) ? true : false; ...