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. 在C语言中实现面向对象(2)

    C语言是结构化和模块化的语言,它是面向过程的.但它也可以模拟C++实现面向对象的功能.那么什么是对象呢?对象就是一个包含数据以及于这些数据有关的操作的集合,也就是包含数据成员和操作代码(即成员函数). ...

  2. Fastqc 碱基质量分布图

    横坐标代表每个每个碱基的位置,反映了读长信息,比如测序的读长为150bp,横坐标就是1到150: 纵坐标代表碱基质量值, 图中的箱线图代表在每个位置上所有碱基的质量值分布, 中间的红线代表的是中位数 ...

  3. 取石子(好学的C++)

    巴什博奕(Bash Game)只有一堆n个物品,两个人轮流从这堆物品中取物,规定每次至少取一个,最多取m个.最后取光者得胜. 显然,如果n=m+1,那么由于一次最多只能取m个,所以,无论先取者拿走多少 ...

  4. php将汉字转换为拼音和得到词语首字母(二)

    <?php class Pinyin{ private $_outEncoding = "GB2312"; public function getPinyin($str,$p ...

  5. 学习 TList 类的实现[7]

    总结目前 TMyList 已具备的功能(3 个方法.3 个属性): Add: 添加; Delete: 删除; Clear: 清空;Count: 元素总数;Capacity: 已存在的所有元素位置数;L ...

  6. VC++ 操作Windows快捷方式

    声明:本文是参考网友博文,然后自己实践整理所得,转载请注明出处! Windows的快捷方式实际上是一个带有扩展名LNK的数据文件,其中包含有用于访问Windows某一对象(即在资源管理器中所能浏览的所 ...

  7. mysql在命令行中,指定要连接的数据库?

    需求描述: mysql客户端,可以在登录到mysql数据库时,指定要连接到哪个数据库 这里进行一个测试. 测试过程: 1.mysql通过-D参数指定连接到test数据库 [mysql@redhat6 ...

  8. c++虚函数表 Brew VTBL

    参考:http://blog.csdn.net/haoel/article/details/1948051/ BREW VTBL:http://blog.chinaunix.net/uid-51740 ...

  9. nginx 杂谈

    http://blog.sina.com.cn/s/articlelist_1834459124_0_1.html

  10. python--dict和set类型--4

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 一.什么是dict 我们已经知道,list 和 tuple 可以用来表示顺序集合,例如,班里同学 ...