hdu4612

Warm up

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 3184    Accepted Submission(s): 720

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

题意:

给定一个联通图,问加入一条边后,最少还余下多少个割边

分析:先求强连通分量个数num,然后缩点形成一棵树,再求树的直径cnt,答案就是num-1-cnt;

程序:

#pragma comment(linker, "/STACK:10240000000000,10240000000000")
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"stack"
#include"iostream"
#define M 201009
#define inf 99999999
using namespace std;
stack<int>q;
struct st
{
int u,v,w,next;
}edge[M*10];
int head[M],use[M],t,dis[M][3],in[M],index,num,belong[M],dfn[M],low[M]; void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
edge[t].u=u;
edge[t].v=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++;
}
void tarjan(int u,int id)
{
dfn[u]=low[u]=++index;
q.push(u);
use[u]=1;
int i;
for(i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(i==(id^1))continue;
if(!dfn[v])
{
tarjan(v,i);
low[u]=min(low[u],low[v]);
}
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
int vv;
num++;
do
{
vv=q.top();
q.pop();
belong[vv]=num;
use[vv]=0;
}while(vv!=u);
}
}
void dfs(int u)
{
use[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!use[v])
{
dfs(v);
//更新最大值和次大值
if(dis[u][0]<dis[v][0]+edge[i].w)
{
int tt=dis[u][0];
dis[u][0]=dis[v][0]+edge[i].w;
dis[u][1]=tt;
}
else if(dis[u][1]<dis[v][0]+edge[i].w)
dis[u][1]=dis[v][0]+edge[i].w;
}
}
if(in[u]==1&&u!=1)//注意
dis[u][0]=dis[u][1]=0;
}
void solve(int n)
{
index=num=0;
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(use,0,sizeof(use));
memset(belong,0,sizeof(belong));
tarjan(1,-1);
}
int uu[M],vv[M];
int main()
{
int n,m,i;
while(scanf("%d%d",&n,&m),m||n)
{
init();
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b,1);
add(b,a,1);
}
solve(n);
int cnt=0;
for(i=0;i<t;i+=2)
{
int u=edge[i].u;
int v=edge[i].v;
if(belong[u]!=belong[v])
{
uu[cnt]=belong[u];
vv[cnt]=belong[v];
cnt++;
}
}
init();
memset(in,0,sizeof(in));
memset(use,0,sizeof(use));
memset(dis,0,sizeof(dis));
for(i=0;i<cnt;i++)
{
//printf("%d %d\n",uu[i],vv[i]);
add(uu[i],vv[i],1);
add(vv[i],uu[i],1);
in[uu[i]]++;
in[vv[i]]++;
}
dfs(1);
int ans=0;
for(i=1;i<=num;i++)
{
if(ans<dis[i][0]+dis[i][1])
ans=dis[i][1]+dis[i][0];
}
printf("%d\n",num-1-ans);
}
return 0;
}

最新文章

  1. maven的聚合与继承
  2. linux内核学习之四 系统调用
  3. 多线程 - CountDownLatch
  4. FastJSON 之bean列表转换为JSON
  5. Redis基础
  6. 将SQL Azure数据库备份到本地SQL Server 2012
  7. CodeForces 589I Lottery (暴力,水题)
  8. 纯原生js移动端图片压缩上传插件
  9. 使用 WPF 创建预加载控件
  10. ubuntu主机名修改
  11. IDEA或Webstorm设置Ctrl+滚轮调整字体大小
  12. PXE 实现自动装机
  13. js实现颜色渐变
  14. 思维导图-mysql log
  15. linux shell $ 特殊变量
  16. Tap 模拟手势点击坐标
  17. will not be exported or published. Runtime ClassNotFoundExceptions may result.
  18. C++ Primer 5th 第18章 用于大型程序的工具
  19. struts通配符*的使用
  20. redis.clients.jedis.HostAndPort - cant resolve localhost address

热门文章

  1. file文件与base64字符串的相互转换
  2. 关于Cocos2d-x项目运行的过程和场景切换步骤
  3. C++ 数据封装
  4. 更改windows 2003远程桌面端口3389为其他的端口号【转】
  5. 使用Grunt构建任务管理脚本(转)
  6. QT把widget转换成图片后打印
  7. Erlang TCP Socket的接收进程的2种方案
  8. 【Java集合的详细研究6】Java 数组
  9. 内网DNS投毒技术劫持会话
  10. [转]Git学习笔记与IntelliJ IDEA整合