Redundant Paths
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11514   Accepted: 4946

Description

In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another.

Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.

There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.

Input

Line 1: Two space-separated integers: F and R

Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.

Output

Line 1: A single integer that is the number of new paths that must be built.

Sample Input

7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7

Sample Output

2
题意:给定一个连通图,问至少加几条边可使这个图为双连通的。
思路:将原图G的双连通分量浓缩为一个点得到图G',则答案为(图G'中的叶子个数+1)/2。
存在重边
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN=;
bool mp[MAXN][MAXN];//存在重边用邻接矩阵,用int的话会MLE
int n,m;
int dfn[MAXN],low[MAXN],time;
int stack[MAXN],top;
int ins[MAXN];
int belong[MAXN],cnt;
void tarjan(int u,int fa)
{
dfn[u]=low[u]=++time;
stack[top++]=u;
ins[u]=true;
for(int v=;v<=n;v++)
{
if(mp[u][v])
{
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
}
else if(v!=fa&&ins[v]) low[u]=min(low[u],dfn[v]);
}
} if(dfn[u]==low[u])//连通分量,发现一个处理一个
{
int v;
cnt++;
do{
v=stack[--top];
ins[v]=false;
belong[v]=cnt;
}while(u!=v);
}
}
int deg[MAXN];
void cal()
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(mp[i][j]&&belong[i]!=belong[j])
{
deg[belong[i]]++;
deg[belong[j]]++;
}
}
int res=;
for(int i=;i<=cnt;i++)
{
if(deg[i]==)
res++;
}
printf("%d\n",(res+)/);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(ins,false,sizeof(ins));
time=;
cnt=;
memset(belong,,sizeof(belong));
memset(mp,false,sizeof(mp));
memset(deg,,sizeof(deg));
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
mp[u][v]=mp[v][u]=true;
}
tarjan(,-);
cal(); }
}

tarjan算法模板:

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN=;
vector<int> mp[MAXN];
int n,m;
int dfn[MAXN],low[MAXN],index;
int bridge[MAXN];
int critical[MAXN];
int root;
int stack[MAXN],top;
bool ins[MAXN];
int belong[MAXN],cnt;
void tarjan(int u,int fa)
{
stack[u]=top++;
ins[u]=true;
dfn[u]=low[u]=++index;
int son=;
for(int i=;i<mp[u].size();i++)
{
int v=mp[u][i];
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
son++;
if((u==root&&son>)||(u!=root&&dfn[u]<=low[v]))
{
critical[u]=;
}
if(dfn[u]<low[v])
{
bridge[v]=;
}
}
else if(v!=fa) low[u]=min(dfn[v],low[u]);
}
if(dfn[u]==low[u])
{
int v;
cnt++;
do{
v=stack[--top];
ins[v]=false;
belong[v]=cnt;
}while(u!=v);
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
mp[u].push_back(v);
mp[v].push_back(u);
}
for(int i=;i<=n;i++)
if(!dfn[i])
{
root=i;
tarjan(i,-);
} printf("割点分别为:\n");
for(int i=;i<=n;i++)
{
if(critical[i])
printf("%d ",i);
}
printf("\n");
printf("割边一端的节点为:\n");
for(int i=;i<=n;i++)
{
if(bridge[i])
printf("%d ",i);
}
printf("\n");
printf("连通分量数目为:\n");
printf("%d\n",cnt);
return ;
}
/*
5 6
1 2
1 3
2 3
3 4
4 5
3 5
*/

POJ3177(无向图变双连通图)的更多相关文章

  1. POJ 3177--Redundant Paths【无向图添加最少的边成为边双连通图 &amp;&amp; tarjan求ebc &amp;&amp; 缩点构造缩点树】

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10798   Accepted: 4626 ...

  2. poj3177 && poj3352 边双连通分量缩点

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12676   Accepted: 5368 ...

  3. poj3177(边双连通分量+缩点)

    传送门:Redundant Paths 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立 ...

  4. POJ3177 Redundant Paths 双连通分量

    Redundant Paths Description In order to get from one of the F (1 <= F <= 5,000) grazing fields ...

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

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

  6. POJ3352Road Construction(构造双连通图)sdut2506完美网络

    构造双连通图:一个有桥的连通图,如何把它通过加边变成边双连通图? 一个有桥的连通图,如何把它通过加边变成边双连通图?方法为首先求出所有的桥,然后删除这些桥边,剩下的每个连通块都是一个双连通子图.把每个 ...

  7. POJ 3177——Redundant Paths——————【加边形成边双连通图】

    Redundant Paths Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  8. Road Construction(无向图的双连通分量)

    http://poj.org/problem?id=3352 题意:给出一个有n个顶点m条边的无向连通图,问至少添加几条边,使删除任意一条边原图仍连通. 思路:一个边双连通图删除任意一条边仍为连通图. ...

  9. [Tarjan系列] Tarjan算法求无向图的双连通分量

    这篇介绍如何用Tarjan算法求Double Connected Component,即双连通分量. 双联通分量包括点双连通分量v-DCC和边连通分量e-DCC. 若一张无向连通图不存在割点,则称它为 ...

随机推荐

  1. kernel&uboot学习笔记

    uboot kernel uboot 1.Uboot编译流程分析: uboot是如何编译生成的? 2.根据include/configs/$(target).h可以生成include/autoconf ...

  2. linux SPI驱动——gpio模拟spi驱动(三)

    一:首先在我的平台注册platform_device,保证能让spi-gpio.c能执行到probe函数. 1: struct spi_gpio_platform_data { 2: unsigned ...

  3. php解析带有命名空间的xml

    xml如果带有命名空间我们将如何解析,例如: <ns1:CreateBillResponse xmlns:ns1="http://neusoft.com" xmlns:xsd ...

  4. php通过curl下载远程图片实例

    <?php $url = 'http://mf1905.com/upload/video_img/df3074c98ec5124ad47c52ff59f74e04_middle.jpeg'; f ...

  5. [转]screen 的使用

    当我们使用securecrt,putty, 等连接远程服务器时,如果正在执行一个程序,比如shell 脚本,退出当前的窗口会导致程序终止!其原理如下: 根据POSIX.1定义: 1 挂断信号(SIGH ...

  6. lnmp建站常识

    1.nginx配置网站目录并修改访问的端口:nginx.conf文件 listen 666;//端口默认为80,修改后增强安全性 server_name www.lnmp.org; index ind ...

  7. TCP(Socket基础编程)

    1.TCP特点: 面向连接.可靠安全.传输量大.速度较慢 2.socket编程主要依靠两个类:socket .serversocket example1:客户端可以不停输入字符串,服务端返回字符串的大 ...

  8. Open-sourcing LogDevice, a distributed data store for sequential data

    https://logdevice.io/blog/2018/09/12/open-sourcing-announcement.html September 12, 2018   We are exc ...

  9. 查看SqlServer安装的log文件

    SqlServer安装时产生的log被保存在这个目录下: "%programfiles%\Microsoft SQL Server\[SQL_VERSION]\Setup Bootstrap ...

  10. Flask的Debug功能非常酷

    Flask是一个Python开发框架.在试用的过程中发现它的debug功能非常cool.如下图所示,在出错的页面每条栈新的右边都有一个按钮,点击之后我们可以执行Python代码,而且非常重要的一点是通 ...