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
*/

最新文章

  1. NHibernate Profiler使用方法
  2. 第 5 章 基础 DOM 和 CSS 操作
  3. Python爬虫个人梳理(代码有空写)
  4. 从源码角度理清memcache缓存服务
  5. 烂泥:vcenter5.5无AD下的安装与配置
  6. iOS学习资源个人整理
  7. 编译Linux系统下的jrtplib3.9和jthread1.3(arm和ubuntu)
  8. 开发XMPP IM
  9. emacs配置详解及C/C++IDE全功能配置演示(附配置文件)
  10. (二十一)unity4.6学习Ugui中文文档-------交互-Supported Events &amp;amp; Raycasters
  11. [转载]PS各个工具的字母快捷键和英文全名
  12. 微信小程序之实现页面缩放式侧滑效果
  13. C语言作业06--结构体&amp;文件
  14. 在java中写出完美的单例模式
  15. java⑧
  16. 数据仓库专题20-案例篇:电商领域数据主题域模型设计v0.2(改进意见征集中)
  17. spring整合quartz时间任务调度框架
  18. Java-01-问题解答
  19. Flask 学习(二)路由
  20. java基础----&gt;Zip压缩的使用

热门文章

  1. Java正确获取客户端真实IP方法整理
  2. Project Euler:Problem 41 Pandigital prime
  3. Python中的注解“@” 、Java 注解
  4. 编译安装Heartbeat常见错误
  5. android菜鸟学习笔记8----Activity(一)
  6. 绿色版Tomcat的配置
  7. 九度OJ 1021:统计字符 (基础题)
  8. json 数据返回解密
  9. 【题解】Sumdiv
  10. 流畅python学习笔记:第十七章:并发处理二