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 条边的无向图,问至少添加几条边,能让该图任意缺少一条边后还能相互连通. 双连通分量定义: 在无向连通图中,如果删除该图的任何一个结点都不能改变该图的连通 ...
随机推荐
- Monkey Tradition(中国剩余定理)
Monkey Tradition Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu Submi ...
- Super Jumping! Jumping! Jumping!(dp)
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- 强制IE浏览器或WebBrowser控件使用指定版本显示网页
自从装了IE10之后,就发现好些个网站显示都不是那么的正常,网站上有些功能竟然还会出现一些意想不到的BUG——本来就是针对IE开发的,现在IE下竟然用不起来了,让用户情何以堪?但是就为少量用户使用的系 ...
- ios获取权限
ios获取权限 by 伍雪颖 -(void)requestRecord{ [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL ...
- 基于RSA的加密/解密示例C#代码
using System;using System.Security.Cryptography;using System.Text; class RSACSPSample{ static void M ...
- [springmvc+mybatis][关于这两个框架的学习,我想说]
关于学习笔记 在对java web有了一定的了解后,这两个框架没怎么写学习笔记了…毕竟项目驱动型…… 关于学习资料 强烈推荐传智播客的燕青讲解的 让我对这种培训班教育的资料刮目相看(不过还是千万别去这 ...
- 几个Linux常见命令
ls 作用:查看目录下的文件 格式:直接ls查看当前所在目录,或者 ls 参数 /目录名 参数: -l 查看详细信息 -a 显示隐藏文件 . 表示当前目录 .. 上级目录 -G 用不同颜色标记 ...
- android——仿微拍贷滑动圆形菜单
一次偶然机会接触到微拍贷的app,瞬间被其圆形可滑动菜单吸引了.一直琢磨着给弄出来. 现在弄出来了.先看看效果吧 如果你也喜欢这个菜单.去我的github找源码吧.太忙了.没时间贴代码和讲解了. ht ...
- ueditor富文本编辑的使用方法
平时在编写上传文件,图片,视频等等这些功能的代码会很繁琐,这里我介绍一款由百度推出的CuteEditor,是一款功能非常强大,支持图片上传.文件下载和word类似的文字编辑器.对于新闻发布系统和博客之 ...
- ios 获取屏幕的属性
屏幕尺寸 CGRect screen = [UIscreen mainScreen].bounds 状态栏尺寸 CGRect rect = [[UIApplication sharedApp ...