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) ...
随机推荐
- Almeza MultiSet Pro(批量安装程序) V8.7.6中文特别版
Almeza MultiSet Pro(批量安装程序)是一款非常实用的工具.它能够帮你批量地安装常用的软件.这将解决每次重装系统后能够快速方便地重装常用软件.使用这款软件不需要编写程序,还可以在安装过 ...
- bzoj 1314: River过河 优先队列
1314: River过河 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 26 Solved: 10[Submit][Status][Discuss ...
- Android编程中常用的PopupWindow和Dialog对话框
注意:PopupWindow组件的使用问题,PopupWindow是一个阻塞对话框,如果你直接在Activity创建的方法中显示它,则会报错:android.view.WindowManager$Ba ...
- JniHelper 含安卓推送
using System; using System.Runtime.CompilerServices; using UnityEngine; internal static class JniHel ...
- Python 标准库 urllib2 的使用细节
刚好用到,这篇文章写得不错,转过来收藏. 转载自 道可道 | Python 标准库 urllib2 的使用细节 Python 标准库中有很多实用的工具类,但是在具体使用时,标准库文档上对使用细节 ...
- IgnoreRoute——注册路由
routes.IgnoreRoute("home/about"); 这句话,当Route遇到Home/About的Url时,这段URL将被忽略. 效果图 需要注意的是这里route ...
- [JAVA]HDU 4919 Exclusive or
题意很简单, 就是给个n, 算下面这个式子的值. $\sum\limits_{i=1}^{n-1} i \otimes (n-i)$ 重点是n的范围:2≤n<10500 比赛的时候 OEIS一下 ...
- 李洪强漫谈iOS开发[C语言-036]-C语言前四天学习小结
- C++如何处理内联虚函数
http://blog.csdn.net/hedylin/article/details/1775556 当一个函数是内联和虚函数时,会发生代码替换或使用虚表调用吗? 为了弄清楚内联和虚函数,让我们将 ...
- 基于ASP.NET的comet简单实现 http长连接,IAsyncResult
http://www.cnblogs.com/hanxianlong/archive/2010/04/27/1722018.html 我潜水很多年,今天忽然出现.很久没写过博客了,不是因为不想写,而是 ...