There are some locations in a park, and some of them are connected by roads. The park manger needs to build some railways along the roads, and he would like to arrange tourist routes to each circuit. If a railway belongs to more than one tourist routes, there might be clash on it, and if a railway belongs to none tourist route, it doesn’t need to build. 
Now we know the plan, and can you tell us how many railways are no need to build and how many railways where clash might happen.

InputThe Input consists of multiple test cases. The first line of each test case contains two integers, n (0 < n <= 10000), m (0 <= m <= 100000), which are the number of locations and the number of the railways. The next m lines, each line contains two integers, u, v (0 <= u, v < n), which means the manger plans to build a railway on the road between u and v. 
You can assume that there is no loop and no multiple edges. 
The last test case is followed by two zeros on a single line, which means the end of the input.OutputOutput the number of railways that are no need to build, and the number of railways where clash might happen. Please follow the format as the sample.Sample Input

8 10
0 1
1 2
2 3
3 0
3 4
4 5
5 6
6 7
7 4
5 7
0 0

Sample Output

1 5

过了一周再来总结这几道题,为什么这道题是每条边访问后就退栈stk[top--],而“warm up”是访问完所以边再退栈。是因为此题不缩点,只是取值,所以不必等访问完所有边再退栈。相反,1-2-4组成一个环,1-3-5组成一个环,点相联通的情况下1-2-4-3-5都应该再这个缩点里,访问完一条边就退栈会导致1-2-4和3-5不在一个缩点里。

比如HDU2242不需要缩点,可以遇到一割边就处理。而HDU4612就必须访问完相连的割边再缩点。

又过了一周,再总结:

   点的双连通存桥(边),每访问一条边操作一次。 

            边的双连通存割点(点),访问完所有边后操作。

尚有疑惑

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=;
const int maxm=;
int Laxt[maxn],Next[maxm],To[maxm],cnt,vis[maxn];
int dfn[maxn],low[maxn];
int times,ans,cute_cnt,n,m;
int q[maxn],q_cnt,scc[maxn],scc_cnt;
int stk[maxn],top;
void _init()
{
memset(Laxt,,sizeof(Laxt));
memset(dfn,,sizeof(dfn));
memset(scc,,sizeof(scc));
memset(vis,,sizeof(vis));
ans=cute_cnt=top=scc_cnt=cnt=times=;
}
void _add(int u,int v)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
}
void _count()//找环
{
int e=;
for(int i=;i<=q_cnt;i++)
for(int j=Laxt[q[i]];j;j=Next[j])
if(scc[To[j]]==scc[q[i]]) e++;
e/=;
if(e>q_cnt) ans+=e;
}
void _tarjan(int u,int v){
dfn[u]=low[u]=++times;
int num_v=;
stk[++top]=u;
for(int i=Laxt[u];i;i=Next[i]){
if(To[i]==v) continue;//此题无重边
if(!dfn[To[i]]){
_tarjan(To[i],u);
if(low[u]>low[To[i]]) low[u]=low[To[i]];
if(low[To[i]]>dfn[u]) cute_cnt++;//割边
if(dfn[u]<=low[To[i]]){//小于是个环,等于是个点,都要处理
q_cnt=;//环内的点
scc_cnt++;
for(;;){
int tmp=stk[top--];
scc[tmp]=scc_cnt;
q[++q_cnt]=tmp;
if(tmp==To[i]) break;
}
scc[u]=scc_cnt;
q[++q_cnt]=u;
_count();
}
}
else if(dfn[To[i]]<low[u]) low[u]=dfn[To[i]];
}
}
int main()
{
int i,j,k,u,v;
while(~scanf("%d%d",&n,&m)){
if(n==&&m==) return ;
_init();
while(m--){
scanf("%d%d",&u,&v);
u++;v++;
_add(u,v);
_add(v,u);
}
for(i=;i<=n;i++)
if(!dfn[i]) _tarjan(i,-);
printf("%d %d\n",cute_cnt,ans);
}
return ;
}

HDU3394Railway Tarjan连通算法的更多相关文章

  1. HDU-2586-How far away(LCA Tarjan离线算法)

    链接:https://vjudge.net/problem/HDU-2586 题意: 勇气小镇是一个有着n个房屋的小镇,为什么把它叫做勇气小镇呢,这个故事就要从勇气小镇成立的那天说起了,修建小镇的时候 ...

  2. LCA问题的ST,tarjan离线算法解法

    一  ST算法与LCA 介绍 第一次算法笔记这样的东西,以前学算法只是笔上画画写写,理解了下,刷几道题,其实都没深入理解,以后遇到新的算法要把自己的理解想法写下来,方便日后回顾嘛>=< R ...

  3. LCA最近公共祖先(Tarjan离线算法)

    这篇博客对Tarjan算法的原理和过程模拟的很详细. 转载大佬的博客https://www.cnblogs.com/JVxie/p/4854719.html 第二次更新,之前转载的博客虽然胜在详细,但 ...

  4. LCA(最近公共祖先)--tarjan离线算法 hdu 2586

    HDU 2586 How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  5. 最近公共祖先LCA Tarjan 离线算法

    [简介] 解决LCA问题的Tarjan算法利用并查集在一次DFS(深度优先遍历)中完成所有询问.换句话说,要所有询问都读入后才开始计算,所以是一种离线的算法. [原理] 先来看这样一个性质:当两个节点 ...

  6. tarjan图论算法

    tarjan图论算法 标签: tarjan 图论 模板 洛谷P3387 [模板]缩点 算法:Tarjan有向图强连通分量+缩点+DAGdp 代码: #include <cstdio> #i ...

  7. LCA 最近公共祖先 Tarjan(离线)算法的基本思路及其算法实现

    首先是最近公共祖先的概念(什么是最近公共祖先?): 在一棵没有环的树上,每个节点肯定有其父亲节点和祖先节点,而最近公共祖先,就是两个节点在这棵树上深度最大的公共的祖先节点. 换句话说,就是两个点在这棵 ...

  8. 半连通分量--Tarjan/Kosaraju算法

    一个有向图称为半连通(Semi-Connected),满足:对于图中任两点u,v,存在一条u到v的有向路径或者从v到u的有向路径. 若满足,则称G’是G的一个导出子图. 若G’是G的导出子图,且G’半 ...

  9. Tarjan系列算法总结(hdu 1827,4612,4587,4005)

    tarjan一直是我看了头大的问题,省选之前还是得好好系统的学习一下.我按照不同的算法在hdu上选题练习了一下,至少还是有了初步的认识.tarjan嘛,就是维护一个dfsnum[]和一个low[],在 ...

随机推荐

  1. 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 Sum

    2017-09-16 12:13:44 writer:pprp 特判 #include <iostream> using namespace std; int main() { int c ...

  2. 解题报告:hdu1248寒冰王座 - 完全背包模板

    2017-09-03 16:16:38 writer:pprp 完全背包问题:从左向右进行扫描,用一维阵列进行分析 代码如下: /* @theme:hdu1248 寒冰王座 @writer:pprp ...

  3. [小问题笔记(十一)] SQL SERVER 将可空字段改为 NOT NULL不可为空的两个方法

    一个字段里面有一些数据是NULL是很讨厌的,写查询麻烦不说,最重要的is null  或者is not null都是不能命中索引的,会导致全表扫描啊. 所以对于一个已经存在NULL的字段,有时间的话最 ...

  4. java自带的MD5

    前言:        MD5可生成16.32.64位数的签名. // MD5加码,32位 public static String toMD5(String plainText) { String r ...

  5. springboot统一异常处理及返回数据的处理

    一.返回code数据的处理 代码: Result.java /** * http请求返回的最外层对象 * Created by 廖师兄 * 2017-01-21 13:34 */ public cla ...

  6. zDialog弹出层插件

    效果图如下: 提取自ZCMS的弹出框: 代替window.open.window.alert.window.confirm:提供良好的用户体验: 水晶质感,设计细腻,外观漂亮: 兼容ie6/7/8.f ...

  7. 英语每日阅读---7、VOA慢速英语(翻译+字幕+讲解):卡梅伦呼吁女性移民学英语

    英语每日阅读---7.VOA慢速英语(翻译+字幕+讲解):卡梅伦呼吁女性移民学英语 一.总结 一句话总结: a.learn English or be deported:British Prime M ...

  8. 威佐夫博弈——hdu1527

    有两堆各若干的物品,两人轮流从其中一堆取至少一件物品,至多不限,或从两堆中同时取相同件物品,规定最后取完者胜利. 直接说结论了,若两堆物品的初始值为(x,y),且x<y,则另z=y-x: 记w= ...

  9. MySQL使用RPM包方式安装

        CentOS7安装MySQL的方法之RPM包方式        

  10. 【css】CSS3 Media Queries 详解【转】

    说起CSS3的新特性,就不得不提到 Media Queries .最近 Max Design 更新的一个泛读列表里,赫然就有关于 Media Queries 的文章.同时位列其中的也有前天我刚刚翻译的 ...