http://acm.hdu.edu.cn/showproblem.php?pid=4612

题目大意:求加一条边最小的桥数

先用Tarjin缩点求出一棵树,然后用bfs求出树的直径,树的直径就是加一条边桥最多的呢条边。

最后就用桥减去直径就行了

Warm up

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 5184    Accepted Submission(s): 1159

Problem Description
  N planets are connected by M bidirectional channels that allow instant transportation. It's always possible to travel between any two planets through these channels.
  If we can isolate some planets from others by breaking only one channel , the channel is called a bridge of the transportation system.
People don't like to be isolated. So they ask what's the minimal number of bridges they can have if they decide to build a new channel.
  Note that there could be more than one channel between two planets.
 
Input
  The input contains multiple cases.
  Each case starts with two positive integers N and M , indicating the number of planets and the number of channels.
  (2<=N<=200000, 1<=M<=1000000)
  Next M lines each contains two positive integers A and B, indicating a channel between planet A and B in the system. Planets are numbered by 1..N.
  A line with two integers '0' terminates the input.
 
Output
  For each case, output the minimal number of bridges after building a new channel in a line.
 
Sample Input
4 4
1 2
1 3
1 4
2 3
0 0
 
Sample Output
0
 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<stack>
#include<queue>
#include<cstdlib> using namespace std;
const int N=;
struct node
{
int next,to;
}edge[N*],Edge[N*];
int Stack[N],low[N],dfn[N],belong[N],sum,Time,top;
int Head[N],head[N],ans,Ans,Max,dis[N],vis[N],End; void Inn()
{
memset(Stack,,sizeof(Stack));
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(Head,-,sizeof(Head));
memset(head,-,sizeof(head));
memset(belong,,sizeof(belong));
memset(dis,,sizeof(dis));
sum=Time=top=ans=Ans=Max=End=;
} void add(int from,int to)
{
edge[ans].to=to;
edge[ans].next=head[from];
head[from]=ans++;
} void Add(int from,int to)
{
Edge[Ans].to=to;
Edge[Ans].next=Head[from];
Head[from]=Ans++;
} void Tarjin(int u,int f)
{
int k=,v;
low[u]=dfn[u]=++Time;
Stack[top++]=u;
for(int i=head[u];i!=-;i=edge[i].next)
{
v=edge[i].to;
if(v==f && !k)
{
k++;
continue;
}
if(!dfn[v])
{
Tarjin(v,u);
low[u]=min(low[u],low[v]);
}
else
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
sum++;
do
{
v=Stack[--top];
belong[v]=sum;
}while(v!=u);
}
} void dfs(int s)
{
queue<int>Q;
int u,v;
memset(vis,,sizeof(vis));
dis[s]=;
vis[s]=;
Q.push(s);
while(Q.size())
{
u=Q.front();
Q.pop();
for(int i=Head[u];i!=-;i=Edge[i].next)
{
v=Edge[i].to;
if(!vis[v])
{
vis[v]=;
dis[v]=dis[u]+;
Q.push(v);
if(Max<dis[v])
{
Max=dis[v];
End=v;
}
}
}
}
} void slove(int n)
{
int SUM=;
Tarjin(,);
for(int i=;i<=n;i++)
{
for(int j=head[i];j!=-;j=edge[j].next)
{
int u=belong[i];
int v=belong[edge[j].to];
if(u!=v)
{
Add(u,v);
SUM++;
}
}
}
SUM/=;
dfs();
dfs(End);
printf("%d\n",SUM-Max);
}
int main()
{
int n,m,a,b;
while(scanf("%d %d",&n,&m),n+m)
{
Inn();
while(m--)
{
scanf("%d %d",&a,&b);
add(a,b);
add(b,a);
}
slove(n);
}
return ;
}

最新文章

  1. MVC如何使用开源分页插件shenniu.pager.js
  2. SharePoint 2013 图文开发系列之WebPart
  3. 关于SQLite的伪随机数
  4. 许愿墙的搭建(基于Apache+php+mysql)
  5. springboot使用之一:连接生产数据库,添加连接池
  6. C#基础----Linq之List&lt;T&gt;篇
  7. 截取视图某一段另存为部分视图(Partial View)
  8. RHCS配置web高可用集群
  9. 如何通过js使搜索关键词高亮
  10. mac下virtualbox配置全网通
  11. [转载]Python实现浏览器自动化操作
  12. package.json和npm install、cnpm install 的問題
  13. 【转】WPS word 文档中的插入对象 为什么打不开
  14. Fillder Script语法
  15. VS Code Html Zen coding
  16. 奔跑吧Linux
  17. vbs习题
  18. React Native 系列(七)
  19. Disconf实践指南:使用篇
  20. Majority Element——算法课上的一道题(经典)

热门文章

  1. es6核心特性-数组扩展
  2. Sql中创建事务处理
  3. Visual SVN自动给文件加锁
  4. iOS Programming Subclassing UITableViewCell
  5. hibernate 批量插入数据
  6. Win10浏览器Spartan无法全屏
  7. patest_1007_Maximum Subsequence Sum_(dp)(思维)
  8. 在Swift中定义属于自己的运算符
  9. cocos2dx游戏如何架构
  10. vs2008如何新建自己工程的环境变量(局部)和 Windows系统(全局). .