poj 3352 双连通分量
至少加几条边成为双连通分量
#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 双连通分量的更多相关文章
- poj 3352 边连通分量
思路与poj3177一模一样. #include<iostream> #include<cstdio> #include<cstring> #include< ...
- POJ 3352 (边双连通分量)
题目链接: http://poj.org/problem?id=3352 题目大意:一个连通图中,至少添加多少条边,使得删除任意一条边之后,图还是连通的. 解题思路: 首先来看下边双连通分量的定义: ...
- POJ 3352 Road Construction(边—双连通分量)
http://poj.org/problem?id=3352 题意: 给出一个图,求最少要加多少条边,能把该图变成边—双连通. 思路:双连通分量是没有桥的,dfs一遍,计算出每个结点的low值,如果相 ...
- Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)
http://blog.csdn.net/geniusluzh/article/details/6619575 在说Tarjan算法解决桥和边双连通分量问题之前我们先来回顾一下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(边双连通分量,桥,tarjan)
题解转自http://blog.csdn.net/lyy289065406/article/details/6762370 文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业 ...
- POJ 3352 Road Construction (边双连通分量)
题目链接 题意 :有一个景点要修路,但是有些景点只有一条路可达,若是修路的话则有些景点就到不了,所以要临时搭一些路,以保证无论哪条路在修都能让游客到达任何一个景点 思路 :把景点看成点,路看成边,看要 ...
- POJ 3352 无向图边双连通分量,缩点,无重边
为什么写这道题还是因为昨天多校的第二题,是道图论,HDU 4612. 当时拿到题目的时候就知道是道模版题,但是苦于图论太弱.模版都太水,居然找不到. 虽然比赛的时候最后水过了,但是那个模版看的还是一知 ...
- 双连通分量 Road Construction POJ - 3352
@[双连通分量] 题意: 有一个 n 个点 m 条边的无向图,问至少添加几条边,能让该图任意缺少一条边后还能相互连通. 双连通分量定义: 在无向连通图中,如果删除该图的任何一个结点都不能改变该图的连通 ...
随机推荐
- sql注入数据库修复方法
1.第一种情况是 需要将指定的 注入字符串全部替换掉(仅替换注入的字符串为空) declare @delStr nvarchar(500) set @delStr='<script src=ht ...
- MP3文件结构及解码概述
MP3文件结构概述 Layer-3音频文件.MPEG(MovingPicture Experts Group)在汉语中译为活动图像专家组,特指活动影音压缩标准,MPEG音频文件是MPEG1标准中的声音 ...
- [core Java学习笔记][第一二三章基本语法]
基本语法 1 Java 简单的类型 1.1 一些常量 正无穷大 Double.POSITVE_INFINITY 负无穷大 Double.NEGATIVE_INFINITY 不存在 Double.NaN ...
- OMX Codec详细解析
概述 OMX Codec是stagefrightplayer中负责解码的模块. 由于遵循openmax接口规范,因此结构稍微有点负责,这里就依照awesomeplayer中的调用顺序来介绍. 主要分如 ...
- js对象克隆, 深复制.
亲测有效: //对象克隆 function clone(obj) { // Handle the 3 simple types, and null or undefined if (null == o ...
- c#修改本地连接工具 ip地址,dns,网关,子网掩码
//Form1类后台 #region 加载配置文件中的信息 /// <summary> /// 加载配置文件中的信息 /// </s ...
- 数据库对于null值的处理
对于null值的处理,不同的数据库的处理函数是不同的,这里列举了部分数据库对于null的处理函数以及使用: Oracle:是用函数nvl(), ----nvl(chinese,0);如果语文成绩为nu ...
- (转) int argc, char* argv[] 的用法
int main(int argc, char* argv[]) 這兩個參數的作用是什麼呢?argc 是指命令行輸入參數的個數,argv存儲了所有的命令行參數.假如你的程式是hello.exe,如果在 ...
- Qt通过odbc读取excel数据
传统的读取方式是通过Excel.Application,这种方式不仅操作繁琐,而且速度也不快. 通过odbc读取,可以使用select语句直接读取整个工作表,处理excel数据就跟数据库一样方便. 当 ...
- (原+转)ubuntu16中安装opencv2.4.11
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5638117.html 参考网址: http://www.cnblogs.com/jeakon/arch ...