Write a program to find the strongly connected components in a digraph.

Format of functions:

void StronglyConnectedComponents( Graph G, void (*visit)(Vertex V) );

where Graph is defined as the following:

typedef struct VNode *PtrToVNode;
struct VNode {
    Vertex Vert;
    PtrToVNode Next;
};
typedef struct GNode *Graph;
struct GNode {
    int NumOfVertices;
    int NumOfEdges;
    PtrToVNode *Array;
};

Here void (*visit)(Vertex V) is a function parameter that is passed into StronglyConnectedComponents to handle (print with a certain format) each vertex that is visited. The function StronglyConnectedComponents is supposed to print a return after each component is found.

Sample program of judge:

#include <stdio.h>
#include <stdlib.h>

#define MaxVertices 10  /* maximum number of vertices */
typedef int Vertex;     /* vertices are numbered from 0 to MaxVertices-1 */
typedef struct VNode *PtrToVNode;
struct VNode {
    Vertex Vert;
    PtrToVNode Next;
};
typedef struct GNode *Graph;
struct GNode {
    int NumOfVertices;
    int NumOfEdges;
    PtrToVNode *Array;
};

Graph ReadG(); /* details omitted */

void PrintV( Vertex V )
{
   printf("%d ", V);
}

void StronglyConnectedComponents( Graph G, void (*visit)(Vertex V) );

int main()
{
    Graph G = ReadG();
    StronglyConnectedComponents( G, PrintV );
    return 0;
}

/* Your function will be put here */

Sample Input (for the graph shown in the figure):

4 5
0 1
1 2
2 0
3 1
3 2

Sample Output:

3
1 2 0

Note: The output order does not matter. That is, a solution like

0 1 2
3

is also considered correct.

这题目就是直接照搬Tarjan算法实现就好了,Tarjan算法在《算法导论》上第22章有,但是我看了以后并没有明白Tarjan算法的过程orz,最后还是看blog看懂的,所以推荐一个讲Tarjan算法讲的很好的blog:http://blog.csdn.net/acmmmm/article/details/16361033 还有Tarjan算法实现的具体代码:http://blog.csdn.net/acmmmm/article/details/9963693 都是一个ACM大佬写的,我就是看这两个的……其实我也看了很久才看懂Tarjan算法是干啥的……毕竟上课从来不听不知道老师讲的方法是怎么样的……

当然只要理解了Tarjan算法,这题目就相当easy了。

补充:还看到一个英文的讲Tarjan的地方,讲的很全面,在geeksforgeeks上http://www.geeksforgeeks.org/tarjan-algorithm-find-strongly-connected-components/

就是打开可能会有点慢,但是不需要FQ。

直接放代码吧:

//
//  main.c
//  Strongly Connected Components
//
//  Created by 余南龙 on 2016/12/6.
//  Copyright © 2016年 余南龙. All rights reserved.
//

int dfn[MaxVertices], low[MaxVertices], stack[MaxVertices], top, t, in_stack[MaxVertices];

int min(int a, int b){
    if(a < b){
        return a;
    }
    else{
        return b;
    }
}

void Tarjan(Graph G, int v){
    PtrToVNode node = G->Array[v];
    int son, tmp;

    dfn[v] = low[v] = ++t;
    stack[++top] = v;
    in_stack[v] = ;

    while(NULL != node){
        son = node->Vert;
         == dfn[son]){
            Tarjan(G, son);
            low[v] = min(low[son], low[v]);
        }
         == in_stack[son]){
            low[v] = min(low[v], dfn[son]);
        }
        node = node->Next;
    }
    if(dfn[v] == low[v]){
        do{
            tmp = stack[top--];
            printf("%d ", tmp);
            in_stack[tmp] = ;
        }while(tmp != v);
        printf("\n");
    }
}

void StronglyConnectedComponents( Graph G, void (*visit)(Vertex V) ){
    int i;

    ; i < MaxVertices; i++){
        dfn[i] = -;
        low[i] = in_stack[i] = ;
    }
    top = -;
    t = ;

    ; i < G->NumOfVertices; i++){
         == dfn[i]){
            Tarjan(G, i);
        }
    }
}

PTA Strongly Connected Components的更多相关文章

  1. Strongly connected components

    拓扑排列可以指明除了循环以外的所有指向,当反过来还有路可以走的话,说明有刚刚没算的循环路线,所以反过来能形成的所有树都是循环

  2. algorithm@ Strongly Connected Component

    Strongly Connected Components A directed graph is strongly connected if there is a path between all ...

  3. [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  4. LeetCode Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  5. [Redux] Using withRouter() to Inject the Params into Connected Components

    We will learn how to use withRouter() to inject params provided by React Router into connected compo ...

  6. [Locked] Number of Connected Components in an Undirected Graph

    Number of Connected Components in an Undirected Graph Given n nodes labeled from 0 to n - 1 and a li ...

  7. cf475B Strongly Connected City

    B. Strongly Connected City time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  8. Strongly connected(hdu4635(强连通分量))

    /* http://acm.hdu.edu.cn/showproblem.php?pid=4635 Strongly connected Time Limit: 2000/1000 MS (Java/ ...

  9. [Swift]LeetCode323. 无向图中的连通区域的个数 $ Number of Connected Components in an Undirected Graph

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

随机推荐

  1. Android学习起步 - AutoCompleteTextView及MultiAutoCompleteTextView控件使用

    大家在用百度搜索时,输入几个词就出现下拉框可选,以下两个控件就具有这个功能 AutoCompleteTextView:只能匹配输入的开始,只有一次匹配 MultiAutoCompleteTextVie ...

  2. sqlce中不支持sp_rename修改表名

    The sp_rename procedure is not avialable in SQL CE! In Sql Server 2005 Management Studio you have to ...

  3. bottlepy template

    bottle template usage 1 example 使用bottle模板,最简单的方法是使用template函数或view装饰器 1.1 template 函数 例子如下: from bo ...

  4. 期待许久的事情终于发生-微软收购Xamarin

    刚在VS推送的新闻中看到了醒目的标题:Microsoft to acquire Xamarin and empower more developers to build apps on any dev ...

  5. android 存储目录

    之前一直不知道 sdcard/Android目录什么作用,我做的项目里面缓存数据到本地一般都是在sdcard上面建一个文件,然后把数据放在这个文件夹下面的子文件夹下.下面介绍一种更好的解决方法. 应用 ...

  6. 在VS中操作Mysql数据库

    1.实现mysql数据库与VS的连接,需要安装两个插件,作者装的是mysql-connector-net-6.9.9.msi和 mysql-for-visualstudio-1.2.6.msi. 2. ...

  7. ArrayList的使用方法(转载)

    转载自: http://i.yesky.com/bbs/jsp/view.jsp?articleID=889992&forumID=150 1.什么是ArrayList    ArrayLis ...

  8. 关于安卓开发当中通过java自带的HttpURLConnection访问XML的java.io.EOFException问题

    刚接触安卓开发,试着写个小程序熟悉下,就写了天气预报的小程序,通过httpUrlConnection读流的方式来获取网络公共接口提供的天气XML信息.但在建立http连接时一直报java.io.EOF ...

  9. SpringMVC无法获取请求中的参数的问题的调查与解决(1)

    *更新:本文第一版中犯了比较大的错误,无论@RequestBody还是@RequestParam注解一样,都会使用全局的Encoding进行解码,会导致特殊编码的参数值丢失. 只要抛弃掉注解,就完全可 ...

  10. OPTIMIZE TABLE 小解

    首先看一下语法:  OPTIMIZE [NO_WRITE_TO_BINLOG | LOCAL] TABLE tbl_name [, tbl_name] ... 我们知道mysql存储引擎里面的数据和索 ...