至少加几条边成为双连通分量

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=10000;
struct
{
int to,next;
}e[maxn];
int head[maxn],lon;
int dfn[maxn],instack[maxn],low[maxn],stack[maxn],s[maxn];
int in[maxn];
int count,top,con;
int n,m;
void edgemake(int from,int to,int head[])
{
e[++lon].to=to;
e[lon].next=head[from];
head[from]=lon;
}
void edgeini()
{
memset(head,-1,sizeof(head));
lon=-1;
} void tarjan(int t,int from)
{
dfn[t]=low[t]=++count;
instack[t]=1;
stack[++top]=t;
for(int k=head[t];k!=-1;k=e[k].next)
{
if(k==(from^1)) continue;
int u=e[k].to;
if(dfn[u]==-1)
{
tarjan(u,k);
low[t]=min(low[t],low[u]);
}
else if(instack[u])
{
low[t]=min(low[t],dfn[u]);
}
}
if(dfn[t]==low[t])
{
++con;
while(1)
{
int u=stack[top--];
instack[u]=0;
s[u]=con;
if(u==t) break;
}
}
} void tarjan()//tarjan的初始化
{
memset(dfn,-1,sizeof(dfn));
memset(instack,0,sizeof(instack));
count=top=con=0;
for(int i=1;i<=n;i++)
if(dfn[i]==-1)
tarjan(i,-1);
} int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
edgeini();
for(int i=1,from,to;i<=m;i++)
{
scanf("%d %d",&from,&to);
edgemake(from,to,head);
edgemake(to,from,head);
}
tarjan();
memset(in,0,sizeof(in));
for(int i=1;i<=n;i++)
for(int k=head[i];k!=-1;k=e[k].next)
if(s[i]!=s[e[k].to])
{
in[s[i]]++;
}
int ans=0;
for(int i=1;i<=con;i++)
if(in[i]==1)
ans++;
else if(in[i]==0)
ans+=2;
if(con==1) ans-=2;
printf("%d\n",(ans+1)/2);
}
return 0;
}

poj 3352 双连通分量的更多相关文章

  1. poj 3352 边连通分量

    思路与poj3177一模一样. #include<iostream> #include<cstdio> #include<cstring> #include< ...

  2. POJ 3352 (边双连通分量)

    题目链接: http://poj.org/problem?id=3352 题目大意:一个连通图中,至少添加多少条边,使得删除任意一条边之后,图还是连通的. 解题思路: 首先来看下边双连通分量的定义: ...

  3. POJ 3352 Road Construction(边—双连通分量)

    http://poj.org/problem?id=3352 题意: 给出一个图,求最少要加多少条边,能把该图变成边—双连通. 思路:双连通分量是没有桥的,dfs一遍,计算出每个结点的low值,如果相 ...

  4. Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)

     http://blog.csdn.net/geniusluzh/article/details/6619575 在说Tarjan算法解决桥和边双连通分量问题之前我们先来回顾一下Tarjan算法是如何 ...

  5. 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 ...

  6. POJ 3352 Road Construction(边双连通分量,桥,tarjan)

    题解转自http://blog.csdn.net/lyy289065406/article/details/6762370   文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业 ...

  7. POJ 3352 Road Construction (边双连通分量)

    题目链接 题意 :有一个景点要修路,但是有些景点只有一条路可达,若是修路的话则有些景点就到不了,所以要临时搭一些路,以保证无论哪条路在修都能让游客到达任何一个景点 思路 :把景点看成点,路看成边,看要 ...

  8. POJ 3352 无向图边双连通分量,缩点,无重边

    为什么写这道题还是因为昨天多校的第二题,是道图论,HDU 4612. 当时拿到题目的时候就知道是道模版题,但是苦于图论太弱.模版都太水,居然找不到. 虽然比赛的时候最后水过了,但是那个模版看的还是一知 ...

  9. 双连通分量 Road Construction POJ - 3352

    @[双连通分量] 题意: 有一个 n 个点 m 条边的无向图,问至少添加几条边,能让该图任意缺少一条边后还能相互连通. 双连通分量定义: 在无向连通图中,如果删除该图的任何一个结点都不能改变该图的连通 ...

随机推荐

  1. eclipse配置maven + 创建maven项目

        登录|注册     努力+坚持,而且还很年轻   目录(?)[+] 在现实的企业中,以低成本.高效率.高质量的完成项目,不仅仅需要技术大牛,企业更加需要管理大牛,管理者只懂技术是远远不够的.当 ...

  2. windows下用vs2008和boost结合编译程序

      原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://co63oc.blog.51cto.com/904636/504469 win ...

  3. Linux下Ant的安装

    OS:CentOS6.3 ant版本:apache-ant-1.9.2-bin 第1步:下载ant apache-ant-1.9.2-bin.tar.gz 第2步:解压 tar -zxvf apach ...

  4. TCP/IP系列——长连接与短连接的区别

    1 什么是长连接和短连接       三次握手和四次挥手   TCP区别于UDP最重要的特点是TCP必须建立在可靠的连接之上,连接的建立和释放就是握手和挥手的过程. 三次握手为连接的建立过程,握手失败 ...

  5. Func 委托 和 Action 委托 初步谈论

    继上篇EventHandler之后,继续填坑,简单了解下Func<TResult> 委托 和 Action 委托. msdn对于两者的解释: Func<TResult>:封装一 ...

  6. Android ---------- 清单文件中Activity常规设置

    <activity android:name="xxxxx" android:alwaysRetainTaskState="true" android:c ...

  7. Lenovo Y430P安装Linux无线网卡

    新买了一台Lenovo Y430P的笔记本,笔记本自带的无线网卡型号是BCM43142.安装了CentOS6.5的操作系统后,按照网上搜索到的网址http://zh-cn.broadcom.com/s ...

  8. 初学linux命令

    linux系统的精髓在于它的命令行 早就听说要学习linux系统,就要学习它的命令行(Command Line Interface).说来惭愧,已经使用了linuxmint快两个月了,虽然能够使用一些 ...

  9. 读jquery.cookie.js源码学到的几个技巧

    一.兼容AMD.CommonJS和普通JS的写法 (function (factory) { if (typeof define === 'function' && define.am ...

  10. [C++程序设计]函数的递归调用

    在调用一个函数的过程中又出现直接或间接地调用 该函数本身,称为函数的递归(recursive)调用. 包含递归调用的函数称为递归函数. 在实现递归时,在时间和空间上的开销比较大 求n! #includ ...