题意:给一个无向图,问最少添加多少条边后能使整个图变成双连通分量。

思路:双连通分量缩点,缩点后给度为1的分量两两之间连边,要连(ans+1) div 2条

low[u]即为u所在的分量编号,flag=0,1,2表示没搜过,没搜完,搜完了

POJ上pascal编译器出问题了不管怎么交都CE

这次写的 应该能处理重边

 var head,vet,next,flag,dfn,low,fan,de:array[..]of longint;
n,m,i,e,v,ans,x,y,tot,time:longint; procedure add(a,b:longint);
begin
inc(tot);
next[tot]:=head[a];
vet[tot]:=b;
head[a]:=tot;
end; function min(x,y:longint):longint;
begin
if x<y then exit(x);
exit(y);
end; procedure dfs(u,le:longint);
var e,v:longint;
begin
flag[u]:=;
inc(time); dfn[u]:=time; low[u]:=time;
e:=head[u];
while e<> do
begin
v:=vet[e];
if e=fan[le] then
begin
e:=next[e];
continue;
end;
if flag[v]= then
begin
dfs(v,e);
low[u]:=min(low[u],low[v]);
end
else if flag[v]= then low[u]:=min(low[u],dfn[v]);
e:=next[e];
end;
flag[u]:=;
end; begin for i:= to do
if i mod = then fan[i]:=i+
else fan[i]:=i-;
while not eof do
begin
readln(n,m);
if (n=)and(m=) then break;
fillchar(head,sizeof(head),);
fillchar(low,sizeof(low),);
fillchar(de,sizeof(de),);
fillchar(flag,sizeof(flag),);
tot:=; time:=;
for i:= to m do
begin
read(x,y);
add(x,y);
add(y,x);
end;
for i:= to n do
if flag[i]= then dfs(i,);
for i:= to n do
begin
e:=head[i];
while e<> do
begin
v:=vet[e];
if low[v]<>low[i] then inc(de[low[i]]);
e:=next[e];
end;
end;
ans:=;
for i:= to n do
if de[i]= then inc(ans);
writeln((ans+) div );
end; end.

这个是去年写的那时候AC了 好像不能处理重边

 var de,low,next,vet,head,flag,dfn:array[..]of longint;
n,m,tot,x,y,i,e,v,leaf,time:longint; function min(x,y:longint):longint;
begin
if x<y then exit(X);
exit(y);
end; procedure add(a,b:longint);
begin
inc(tot);
next[tot]:=head[a];
vet[tot]:=b;
head[a]:=tot;
end; procedure dfs(u,fa:longint);
var e,v:longint;
begin
inc(time);
dfn[u]:=time; low[u]:=time;
flag[u]:=;
e:=head[u];
while e<> do
begin
v:=vet[e];
if v=fa then
begin
e:=next[e];
continue;
end;
if flag[v]= then
begin
dfs(v,u);
low[u]:=min(low[u],low[v]);
end
else if flag[v]= then low[u]:=min(low[u],dfn[v]);
e:=next[e];
end;
flag[u]:=;
end; begin while not eof do
begin
readln(n,m);
if (n=)and(m=) then break;
fillchar(head,sizeof(head),);
tot:=;
for i:= to m do
begin
readln(x,y);
add(x,y);
add(y,x);
end;
fillchar(dfn,sizeof(dfn),);
fillchar(de,sizeof(de),);
fillchar(flag,sizeof(flag),);
time:=;
for i:= to n do
if flag[i]= then dfs(i,i);
for i:= to n do
begin
e:=head[i];
while e<> do
begin
v:=vet[e];
if low[i]<>low[v] then inc(de[low[i]]);
e:=next[e];
end;
end;
leaf:=;
for i:= to n do
if de[i]= then inc(leaf);
writeln((leaf+) div );
end; end.

UPD(2018.10.18):C++

 #include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair
#define N 15000
#define M 6100000
#define eps 1e-8
#define pi acos(-1)
#define oo 1e9 int head[N],vet[N],nxt[N],flag[N],dfn[N],low[N],fan[N],d[N],
tot,tim; void add(int a,int b)
{
nxt[++tot]=head[a];
vet[tot]=b;
head[a]=tot;
} void dfs(int u,int le)
{
flag[u]=;
dfn[u]=low[u]=++tim;
int e=head[u];
while(e)
{
int v=vet[e];
if(e==fan[le])
{
e=nxt[e];
continue;
}
if(!flag[v])
{
dfs(v,e);
low[u]=min(low[u],low[v]);
}
else if(flag[v]==) low[u]=min(low[u],dfn[v]);
e=nxt[e];
}
flag[u]=;
} int main()
{
freopen("poj3352.in","r",stdin);
freopen("poj3352.out","w",stdout);
for(int i=;i<=;i++)
if(i&) fan[i]=i+;
else fan[i]=i-;
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(head,,sizeof(head));
memset(low,,sizeof(low));
memset(d,,sizeof(d));
memset(flag,,sizeof(flag));
tot=tim=;
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
for(int i=;i<=n;i++)
if(flag[i]==) dfs(i,);
for(int i=;i<=n;i++)
{
int e=head[i];
while(e)
{
int v=vet[e];
if(low[v]!=low[i]) d[low[i]]++;
e=nxt[e];
}
}
int ans=;
for(int i=;i<=n;i++)
if(d[i]==) ans++;
printf("%d\n",(ans+)/);
}
return ;
}

【POJ3352】Road Construction(边双联通分量)的更多相关文章

  1. [POJ3352]Road Construction

    [POJ3352]Road Construction 试题描述 It's almost summer time, and that means that it's almost summer cons ...

  2. POJ-3352-RoadConstruction(边双联通分量,缩点)

    链接:https://vjudge.net/problem/POJ-3352#author=0 题意: 给一个无向连通图,至少添加几条边使得去掉图中任意一条边不改变图的连通性(即使得它变为边双连通图) ...

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

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

  4. 【UVA10972】RevolC FaeLoN (求边双联通分量)

    题意: 给你一个无向图,要求把所有无向边改成有向边,并且添加最少的有向边,使得新的有向图强联通. 分析: 这题的解法还是很好想的.先用边双联通分量缩点,然后找新图中入度为0和为1的点,入度为0则ans ...

  5. lightoj 1300 边双联通分量+交叉染色求奇圈

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1300 边双连通分量首先dfs找出桥并标记,然后dfs交叉着色找奇圈上的点.这题只要求在 ...

  6. HDU5409---CRB and Graph 2015多校 双联通分量缩点

    题意:一个联通的无向图, 对于每一条边, 若删除该边后存在两点不可达,则输出这两个点, 如果存在多个则输出第一个点尽可能大,第二个点尽可能小的. 不存在输出0 0 首先 若删除某一条边后存在多个联通分 ...

  7. poj2942(双联通分量,交叉染色判二分图)

    题意:一些骑士,他们有些人之间有矛盾,现在要求选出一些骑士围成一圈,圈要满足如下条件:1.人数大于1.2.总人数为奇数.3.有仇恨的骑士不能挨着坐.问有几个骑士不能和任何人形成任何的圆圈. 思路:首先 ...

  8. 『Tarjan算法 无向图的双联通分量』

    无向图的双连通分量 定义:若一张无向连通图不存在割点,则称它为"点双连通图".若一张无向连通图不存在割边,则称它为"边双连通图". 无向图图的极大点双连通子图被 ...

  9. 大白书中无向图的点双联通分量(BCC)模板的分析与理解

    对于一个无向图,如果任意两点至少存在两条点不重复(除起点和终点外无公共点)的路径,则这个图就是点双联通. 这个要求等价于任意两条边都存在于一个简单环(即同一个点不能在圈中出现两次)中,即内部无割点. ...

  10. 洛谷P2860 [USACO06JAN]冗余路径Redundant Paths(tarjan求边双联通分量)

    题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...

随机推荐

  1. java基础—this关键字

    一.this关键字

  2. LiteIDE 错误: 进程无法启动

    问题 运行 01_hello.go,提示以下错误 新建文件夹().exe [C:/Users/Administrator/Desktop/新建文件夹()] 错误: 进程无法启动. 原因 工程目录名不能 ...

  3. Java中什么是匿名对象,空参构造方法输出创建了几个匿名对象,属性声明成static

    package com.swift; //使用无参构造方法自动生成对象,序号不断自增 public class Person { private static int count; //如果在定义类时 ...

  4. (转发)IOS高级开发~Runtime(三)

    11.系统类的方法实现部分替换 - (void) methodExchange { Method m1 = class_getInstanceMethod([NSStringclass],@selec ...

  5. C#经典面试题——递归运算

    今天开始写递归,然而始终不得甚解.借鉴别人的理解:假设我们现在都不知道什么是递归,我们自然想到打开浏览器,输入到谷歌的网页,我们点击搜索递归,然后我们在为维基百科中了解到了递归的基本定义,在了解到了递 ...

  6. 学习笔记(四): Representation:Feature Engineering/Qualities of Good Features/Cleaning Data/Feature Sets

    目录 Representation Feature Engineering Mapping Raw Data to Features Mapping numeric values Mapping ca ...

  7. 【期望dp】bzoj4832: [Lydsy1704月赛]抵制克苏恩

    这个题面怎么这么歧义…… Description 小Q同学现在沉迷炉石传说不能自拔.他发现一张名为克苏恩的牌很不公平.如果你不玩炉石传说,不必担心,小Q 同学会告诉你所有相关的细节.炉石传说是这样的一 ...

  8. pythonnet-网络编程(1)

    python的网络编程有不少难点,也容易忘记,最近我会陆续发出系统.完整pythonnet知识的博客,一边复习一边分享,感兴趣的可以关注我. 话不多说,开始吧. 网络编程 目的:数据的传输 ISO(国 ...

  9. http 基础与通讯原理

    目录 http 基础与通讯原理 Internet 与中国 1990年10月 注册CN顶级域名 1993年3月2日 接入第一根专线 1994年4月20日 实现与互联网的全功能连接 1994年5月21日 ...

  10. Node项目实战-静态资源服务器

    打开github,在github上创建新项目: Repository name: anydoor Descripotion: Tiny NodeJS Static Web server 选择:publ ...