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;
}

tarjan算法求缩点+树形DP求直径的更多相关文章

  1. 浅谈关于树形dp求树的直径问题

    在一个有n个节点,n-1条无向边的无向图中,求图中最远两个节点的距离,那么将这个图看做一棵无根树,要求的即是树的直径. 求树的直径主要有两种方法:树形dp和两次bfs/dfs,因为我太菜了不会写后者这 ...

  2. HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...

  3. 洛谷 P2515 [HAOI2010]软件安装(缩点+树形dp)

    题面 luogu 题解 缩点+树形dp 依赖关系可以看作有向边 因为有环,先缩点 缩点后,有可能图不联通. 我们可以新建一个结点连接每个联通块. 然后就是树形dp了 Code #include< ...

  4. hdu6446 网络赛 Tree and Permutation(树形dp求任意两点距离之和)题解

    题意:有一棵n个点的树,点之间用无向边相连.现把这棵树对应一个序列,这个序列任意两点的距离为这两点在树上的距离,显然,这样的序列有n!个,加入这是第i个序列,那么这个序列所提供的贡献值为:第一个点到其 ...

  5. 2017 Wuhan University Programming Contest (Online Round) B Color 树形dp求染色方法数

    /** 题目:Color 链接:https://oj.ejq.me/problem/23 题意:给定一颗树,将树上的点最多染成m种颜色,有些节点不可以染成某些颜色.相邻节点颜色不同.求染色方法数. 思 ...

  6. HDU - 3899 JLUCPC(树形dp求距离和)

    JLUCPC Dr. Skywind and Dr. Walkoncloud are planning to hold the annual JLU Collegiate Programming Co ...

  7. 洛谷P2515 [HAOI2010]软件安装(tarjan缩点+树形dp)

    传送门 我们可以把每一个$d$看做它的父亲,这样这个东西就构成了一个树形结构 问题是他有可能形成环,所以我们还需要一遍tarjan缩点 缩完点后从0向所有入度为零的点连边 然后再跑一下树形dp就行了 ...

  8. BZOJ 2427 /HAOI 2010 软件安装 tarjan缩点+树形DP

    终于是道中文题了.... 当时考试的时候就考的这道题.... 果断GG. 思路: 因为有可能存在依赖环,所以呢 先要tarjan一遍 来缩点. 随后就进行一遍树形DP就好了.. x表示当前的节点.j表 ...

  9. 树形DP求树的重心 --SGU 134

    令一个点的属性值为:去除这个点以及与这个点相连的所有边后得到的连通分量的节点数的最大值. 则树的重心定义为:一个点,这个点的属性值在所有点中是最小的. SGU 134 即要找出所有的重心,并且找出重心 ...

随机推荐

  1. glob函数 循环遍历子目录下的文件

    <?php foreach (glob("ueditor\php\upload\image\*\*.png") as $filename) { echo "$fil ...

  2. 关于Cocos2d-x有些头文件无法引入或者类显示无法打开

    选中工程右键“属性”->"配置属性“->"c/c++"->"常规”->"附加包含目录"中添加“”$(EngineRo ...

  3. (转)非阻塞Connect对于select时应注意问题

    对于面向连接的socket类型(SOCK_STREAM,SOCK_SEQPACKET)在读写数据之前必须建立连接,首先服务器端socket必须在一个客户端知道的地址进行监听,也就是创建socket之后 ...

  4. 如何POST一个JSON格式的数据给Restful服务

    在Android/java平台上实现POST一个json数据: JSONObject jsonObj = new JSONObject(); jsonObj.put("username&qu ...

  5. NHibernate初学六之关联多对多关系

    1:创建三张表T_Course.T_Student.T_Middle:其中一个学生可以对应多个课程,一个课程也可以对应多个学生,用T_Middle存放它们的关系内容: CREATE TABLE [db ...

  6. Ini操作类

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  7. Python 爬虫知识点 - 淘宝商品检索结果抓包分析(续一)

    通过前一节得出地址可能的构建规律,如下: https://s.taobao.com/search?data-key=s&data-value=44&ajax=true&_ksT ...

  8. CSDN专栏收集

     Android集 1.Himi李华明的<Android游戏开发专栏>http://blog.csdn.net/column/details/androidgame.html2.老罗的&l ...

  9. c++11——type_traits 类型萃取

    一. c++ traits traits是c++模板编程中使用的一种技术,主要功能:     把功能相同而参数不同的函数抽象出来,通过traits将不同的参数的相同属性提取出来,在函数中利用这些用tr ...

  10. 【PHP】使用openssl进行Rsa长数据加密(117)解密(128)

    PHP使用openssl进行Rsa加密,如果要加密的明文太长则会出错,解决方法:加密的时候117个字符加密一次,然后把所有的密文拼接成一个密文:解密的时候需要128个字符解密一下,然后拼接成数据. 加 ...