Tarjan算法解析(转)附POJ -- 3352
来源:互联网 发布:unity3d旧版动画系统 编辑:程序博客网 时间:2024/05/16 12:58
Tarjan算法详解
【功能】
Tarjan算法的用途之一是,求一个有向图G=(V,E)里极大强连通分量。强连通分量是指有向图G里顶点间能互相到达的子图。而如果一个强连通分量已经没有被其它强通分量完全包含的话,那么这个强连通分量就是极大强连通分量。
【算法思想】
用dfs遍历G中的每个顶点,通dfn[i]表示dfs时达到顶点i的时间,low[i]表示i所能直接或间接达到时间最小的顶点。(实际操作中low[i]不一定最小,但不会影响程序的最终结果)
程序开始时,time初始化为0,在dfs遍历到v时,low[v]=dfn[v]=time++,
v入栈(这里的栈不是dfs的递归时系统弄出来的栈)扫描一遍v所能直接达到的顶点k,如果 k没有被访问过那么先dfs遍历k,low[v]=min(low[v],low[k]);如果k在栈里,那么low[v]=min(low[v],dfn[k])(就是这里使得low[v]不一定最小,但不会影响到这里的low[v]会小于dfn[v])。扫描完所有的k以后,如果low[v]=dfn[v]时,栈里v以及v以上的顶点全部出栈,且刚刚出栈的就是一个极大强连通分量。
【大概的证明】
1. 在栈里,当dfs遍历到v,而且已经遍历完v所能直接到达的顶点时,low[v]=dfn[v]时,v一定能到达栈里v上面的顶点:
因为当dfs遍历到v,而且已经dfs递归调用完v所能直接到达的顶点时(假设上面没有low=dfn),这时如果发现low[v]=dfn[v],栈上面的顶点一定是刚才从顶点v递归调用时进栈的,所以v一定能够到达那些顶点。
2 .dfs遍历时,如果已经遍历完v所能直接到达的顶点而low[v]=dfn[v],我们知道v一定能到达栈里v上面的顶点,这些顶点的low一定小于 自己的dfn,不然就会出栈了,也不会小于dfn[v],不然low [v]一定小于dfn[v],所以栈里v以其v以上的顶点组成的子图是一个强连通分量,如果它不是极大强连通分量的话low[v]也一定小于dfn[v](这里不再详细说),所以栈里v以其v以上的顶点组成的子图是一个极大强连通分量。
【时间复杂度】
因为所有的点都刚好进过一次栈,所有的边都访问的过一次,所以时间复杂度为O(n+m)
以上内容转自:http://blog.sina.com.cn/s/blog_69a101130100k1cm.html
PS:
Tarjan算法同时可用于求出无向连通图的割点和割边(桥)
POJ 3352 属于最基础的Tarjan算法应用,题目如下:
Description
It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.
The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.
Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.
So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.
Input
The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 to n. Each of the following r lines will consist of two integers, v and w, separated by a space, indicating that a road exists between the attractions labelled v and w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.
Output
One line, consisting of an integer, which gives the minimum number of roads that we need to add.
Sample Input
10 121 21 31 42 52 65 63 73 87 84 94 109 103 31 22 31 3
Sample Output
20
大致题意是:各个点之间有边相连,问你要添加多少条边才能使这个图成为双连通图。
思路:找出所有割边(桥),则这些割边可以把原来的图分成几个子图,然后进行缩点操作,既把所有子图看成一个点,这样便将原来的图变成一个只含有割边的图。然后找出入度为1的点的数量num,则需要添加的边为
(num+1)/2。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <stack> 6 #define MAX 1005 7 8 using namespace std; 9 10 struct node11 {12 int to,next;13 };14 15 node point[MAX*100];16 int head[MAX];17 int dfn[MAX],low[MAX],degree[MAX];18 bool used[MAX][MAX];19 int N,M,index,time,sum;20 21 void addNode(int from,int to)22 {23 point[index].to=to;24 point[index].next=head[from];25 head[from]=index++;26 }27 28 void tarjan(int cur,int father)29 {30 dfn[cur]=low[cur]=++time;31 for(int i=head[cur];i!=-1;i=point[i].next)32 {33 int cur_to=point[i].to;34 if(!dfn[cur_to])35 {36 tarjan(cur_to,cur);37 low[cur]=min(low[cur],low[cur_to]);38 }39 else if(cur_to!=father)40 low[cur]=min(low[cur],dfn[cur_to]);41 }42 }43 44 int solve()45 {46 int ret = 0;47 memset(degree,0,sizeof(degree));48 for(int i=1;i<=N;i++)49 {50 for(int j=head[i];j!=-1;j=point[j].next)51 {52 int cur=point[j].to;53 if(low[cur]!=low[i])54 {55 degree[low[i]]++;56 }57 }58 }59 for(int i=0;i<=N;i++)60 if(degree[i]==1)61 ret ++;62 return (ret+1)/2;63 }64 65 int main()66 {67 while(~scanf("%d%d",&N,&M))68 {69 index=0;70 time=0;71 memset(head,-1,sizeof(head));72 memset(used,0,sizeof(used));73 for(int i=1;i<=M;i++)74 {75 int s,e;76 scanf("%d%d",&s,&e);77 if(!used[s][e])78 {79 addNode(s,e);80 addNode(e,s);81 used[s][e]=used[e][s]=true;82 }83 }84 memset(dfn,0,sizeof(dfn));85 tarjan(1,1);86 int ans = solve();87 printf("%d\n",ans);88 }89 return 0;90 }
- Tarjan算法解析(转)附POJ -- 3352
- Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)
- Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)
- (算法)Tarjan离线算法解决LCA问题 (附POJ 1470 Closest Common Ancestors 代码)
- (算法)Tarjan离线算法解决LCA问题 (附POJ 1470 Closest Common Ancestors 代码)
- Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告
- poj 1236(tarjan算法)
- poj 3352(tarjan)
- poj 2186(tarjan 算法实现)
- POJ 1253 SPF(tarjan算法求割点)
- POJ 1523-SPF(Tarjan算法-关节点)
- poj 2553 tarjan算法
- poj 2186 tarjan算法
- poj--1470--tarjan算法
- POJ 1470 Tarjan算法
- POJ 1236 Tarjan算法
- poj 3117&&poj 3352 (边双连通分量+缩点 Tarjan算法 )
- LCA之Tarjan算法解析
- 学习图算法----最短路径问题
- POJ-3281 : 最大流(Dinic算法 + ISAP算法)
- HDU 1498 -- 二分图的最大匹配
- HDU--2780
- POJ -- 1948 二维背包问题
- Tarjan算法解析(转)附POJ -- 3352
- POJ 2942 ( 补图+判断奇圈(交叉染色法)+Tarjan(点连通分量))
- HDU 4027 ( 线段树 -- 成段更新)
- HDU 1892 (二维树状数组)
- 嵌入式Linux操作系统名词解释及资源大全
- POJ 2455 二分+最大流
- HDU 3416 最短路+最大流
- HDU 3667 最小费用最大流+拆边
- POJ 3686 最小费用最大流