hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】
Warm up
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 5093 Accepted Submission(s):
1131
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.
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.
after building a new channel in a line.
//scc代表 双联通分量 写习惯了 顺手就写成scc了也懒得改了
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#define MAXM 2000100
#define MAX 200100
#define INF 0x7fffff
using namespace std;
int n,m,bridge,sum;
int low[MAX],dfn[MAX];
int head[MAX],ans,age;
int sccno[MAX];//代表当前点属于哪个双连通分量
int dfsclock,scccnt;
vector<int>newmap[MAX];//储存缩点后的新图
int instack[MAX];//标记当前点是否入栈
int dis[MAX];//求树的直径时记录路径的长度
int vis[MAX];//求树的直径时标记是否入队列
stack<int>s;
struct node
{
int beg,end,next;
}edge[MAXM];
void init()
{
ans=0;
bridge=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
edge[ans].beg=u;
edge[ans].end=v;
edge[ans].next=head[u];
head[u]=ans++;
}
void getmap()
{
int a,b;
while(m--)
{
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
}
void tarjan(int u,int fa)
{
int v,i;
low[u]=dfn[u]=++dfsclock;
instack[u]=1;
s.push(u);
int flag=1;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].end;
if(flag&&v==fa)//判重边
{
flag=0;
continue;
}
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(dfn[u]<low[v])
bridge++;
}
else if(instack[v])
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
scccnt++;
while(1)
{
v=s.top();
s.pop();
instack[v]=0;
sccno[v]=scccnt;
if(v==u)
break;
}
}
}
void find()
{
int i;
memset(low,0,sizeof(low));
memset(sccno,0,sizeof(sccno));
memset(dfn,0,sizeof(dfn));
memset(instack,0,sizeof(instack));
dfsclock=scccnt=0;
for(i=1;i<=n;i++)
{
if(!dfn[i])
tarjan(i,-1);
}
}
void suodian()
{
int u,v,i,j;
for(i=1;i<=scccnt;i++)
newmap[i].clear();
for(i=0;i<ans;i=i+2)
{
u=sccno[edge[i].beg];
v=sccno[edge[i].end];
if(u!=v)
{
newmap[u].push_back(v);
newmap[v].push_back(u);
}
}
}
void bfs(int beg)
{
queue<int>q;
memset(dis,0,sizeof(dis));
memset(vis,0,sizeof(vis));
int i,j;
while(!q.empty())
q.pop();
sum=0;
age=beg;
vis[beg]=1;
q.push(beg);
int u;
while(!q.empty())
{
u=q.front();
q.pop();
for(i=0;i<newmap[u].size();i++)
{
if(!vis[newmap[u][i]])
{
dis[newmap[u][i]]=dis[u]+1;
vis[newmap[u][i]]=1;
q.push(newmap[u][i]);
if(sum<dis[newmap[u][i]])
{
sum=dis[newmap[u][i]];
age=newmap[u][i];
}
}
}
}
}
void solve()
{
int i,j;
bfs(1);
bfs(age);
printf("%d\n",bridge-sum);
//printf("%d\n%d\n",bridge,sum);
}
int main()
{
while(scanf("%d%d",&n,&m),n|m)
{
init();
getmap();
find();
//printf("%d#\n",bridge);
suodian();
solve();
}
return 0;
}
hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】的更多相关文章
- hdoj 3861 The King’s Problem【强连通缩点建图&&最小路径覆盖】
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 4612 Warm up 双连通缩点+树的直径
首先双连通缩点建立新图(顺带求原图的总的桥数,事实上因为原图是一个强连通图,所以桥就等于缩点后的边) 此时得到的图类似树结构,对于新图求一次直径,也就是最长链. 我们新建的边就一定是连接这条最长链的首 ...
- HDU 1827 Summer Holiday(tarjan求强连通分量+缩点构成新图+统计入度+一点贪心思)经典缩点入门题
Summer Holiday Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 4612 Warm up 双连通+树形dp思想
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total S ...
- POJ 3352 Road Construction(边双连通分量,桥,tarjan)
题解转自http://blog.csdn.net/lyy289065406/article/details/6762370 文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业 ...
- HDU-4612 Warm up,tarjan求桥缩点再求树的直径!注意重边
Warm up 虽然网上题解这么多,感觉写下来并不是跟别人竞争访问量的,而是证明自己从前努力过,以后回头复习参考! 题意:n个点由m条无向边连接,求加一条边后桥的最少数量. 思路:如标题,tarjan ...
- 2013杭州网赛 1001 hdu 4738 Caocao's Bridges(双连通分量割边/桥)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:有n座岛和m条桥,每条桥上有w个兵守着,现在要派不少于守桥的士兵数的人去炸桥,只能炸一条桥 ...
- poj3694+hdu2460 求桥+缩点+LCA/tarjan
这个题使我更深理解了TARJAN算法,题意:无向图,每添加一条边后文桥的数量,三种解法:(按时间顺序),1,暴力,每每求桥,听说这样能过,我没过,用的hash判重,这次有俩个参数(n->10w, ...
- hdu4738(边双连通分量,桥)
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- homework-Agile Software Development
对敏捷开发的一些思考 这周的作业是对敏捷开发的相关阅读和思考. 在阅读的过程中,可以看到作者是一位具有丰富编程经验的大师.在开发的经历中,作者经历了极限编程等开发过程,但是在作者的多年经验中,作者还是 ...
- pair work-Elevator Schedule附加题
[电梯调度算法的实现和测试] [附加题] 首先,我要感谢周敏轩同学和薛亚杰,吴渊渊小组.UI的编写是在两个小组成员的共同努力下完成的,希望在第二次结对编程中能够再一起对UI界面进行更新和完善.UI编写 ...
- OpenCV学习-b
OpenCV是开源计算机视觉和机器学习库.包含成千上万优化过的算法.项目地址:http://opencv.org/about.html.官方文档:http://docs.opencv.org/modu ...
- Python属性、方法和类管理系列之----元类
元类的介绍 请看位于下面网址的一篇文章,写的相当好. http://blog.jobbole.com/21351/ 实例补充 class Meta(type): def __new__(meta, c ...
- c语言的自动类型转换
转自c语言的自动类型转换 自动转换遵循以下规则: 1) 若参与运算量的类型不同,则先转换成同一类型,然后进行运算. 2) 转换按数据长度增加的方向进行,以保证精度不降低.如 ...
- 如何让Activiti-Explorer使用sql server数据库
从官网下载的Activiti-explorer的war文件内部默认是使用h2内存数据库的,如果想改用其他的数据库来做持久化,比如sql server,需要做如下配置. 1)修改db.propertie ...
- linux下安装jira详细步骤
首先从官网下载jdk的安装包,将jdk的安装包上传到虚拟机或者服务器,在./usr/local/目录下面创建一个java目录:mkdir java 等等,具体祥看本文,希望对你有所帮助 linux下安 ...
- MSSQL版本
(1)661是sql2008 R2的版本号 Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (Intel X86) Apr 2 201 ...
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-003- 上传文件multipart,配置StandardServletMultipartResolver、CommonsMultipartResolver
一.什么是multipart The Spittr application calls for file uploads in two places. When a new user register ...
- 145. Binary Tree Postorder Traversal
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...