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) ...
随机推荐
- TOKEN+签名验证
TOKEN+签名验证 首先问大家一个问题,你在写开放的API接口时是如何保证数据的安全性的?先来看看有哪些安全性问题在开放的api接口中,我们通过http Post或者Get方式请求服务器的时候,会面 ...
- Jfinal 入门
Jfinal 入门 IDE----->IDEA 新建项目 新建web项目 添加maven特性 方便导入jar包,不用一个个导入了 配置pom.xml <dependencies> & ...
- 如何确定照片是否被PS过
除了用软件,还可以先右键属性----解除锁定----重新打开属性看详细信息.
- hdu 3715
一个很简单的2-sat的题: 不过比较难想到: 其实也不是很难,可能接触的少了吧! #include<cstdio> #include<vector> #define maxn ...
- Docker 监控- Prometheus VS Cloud Insight
如今,越来越多的公司开始使用 Docker 了,2 / 3 的公司在尝试了 Docker 后最终使用了它.为了能够更精确的分配每个容器能使用的资源,我们想要实时获取容器运行时使用资源的情况,怎样对 D ...
- HDU 1171 Big Event in HDU(DP)
点我看题目 题意 : 给你一个n,然后n组数据,每组两个数字,一个是物品的价值,另外一个是物品的数量,让你尽量将这些东西分成价值相等的两份,如果无法相等就前一份要大于后一份. 思路 :这个题可以转化成 ...
- SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-010-Introduction为类增加新方法@DeclareParents、<aop:declare-parents>
一. 1.Introduction的作用是给类动态的增加方法 When Spring discovers a bean annotated with @Aspect , it will automat ...
- Google的代码风格规范,各种语言都很全
https://code.google.com/p/google-styleguide/
- 优化 Android 线程和后台任务开发
在 Android 开发中,你不应该做任何阻碍主线程的事情.但这究竟意味着什么呢?在这次海湾 Android 开发者大会讲座中,Ari Lacenski 认为对于长时间运行或潜在的复杂任务要特别小心. ...
- 【PythonChallenge】Level 3
题目为正则表达式,需要注意EXACTLY的含义,即AAAAxBBB中x不满足条件,但aAAAxBBBa却满足条件.使用perl解决此题,利用s///替换字母,循环读取整个源码文件,结果为linkedl ...