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. 在 Hibernate 中出现 database product name cannot be null 时怎么解决?

    今 天在做一个SH项目结合的时候忽然出现了,这样的错误,我开始也不知道怎么办,便上网查,看一些高手回答都是说,检查 hibernate.cfg.xml 这个配置文件,或是一些其它的配置,于是我便看了一 ...

  2. win10 MySQL启动失败问题

    系统升级到win10之后,本地装的MySQL却突然不能启动,系统显示明明就有,可是总是启动失败.在这里解决一下: 解决win10  mysql服务消失,连接不上的问题,注意:以管理员身份运行DOS命令 ...

  3. 二模12day2解题报告

    T1.笨笨玩糖果(sugar) 有n颗糖,两个人轮流取质数颗糖,先取不了的(0或1)为输,求先手能否必胜,能,输出最少几步肯定能赢:不能,输出-1. 一开始天真的写了一个dp,f[i]表示i颗糖最少取 ...

  4. Scorpio-CSharp简介

    Scorpio-CSharp是为了解决Unity游戏各个平台热更新的问题,纯c#实现 基于.net2.0 兼容所有c#平台 语法类似 javascript, 设计初衷是为了做一个所有人都能修改的热更新 ...

  5. Python字符串的encode与decode研究心得乱码问题解决方法

    为什么Python使用过程中会出现各式各样的乱码问题,明明是中文字符却显示成“\xe4\xb8\xad\xe6\x96\x87”的形式? 为什么会报错“UnicodeEncodeError: 'asc ...

  6. U盘FAT32文件系统转换成NTFS文件系统

    首先 点击-开始--运行---输入CMD ----输入convert X:/FS:NTFS 其中 X是U盘所在的盘符

  7. PS Web切图界面设置

    界面为移动工具时(快捷键V),选中左上角的图层. 点击视图,选中显示→智能参考线,与标尺. 点击窗口,把"库" "颜色"去掉,把屏幕右上角的"通道&q ...

  8. sql2000添加表注释,列注释 及修改 删除 注释

    --创建表--create table 表(a1 varchar(10),a2 char(2)) --为表添加描述信息EXECUTE sp_addextendedproperty 'MS_Descri ...

  9. 思达index网站

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. SqlLocalDB使用笔记

    一.介绍 SqlLocalDB是VS安装时附带的数据库软件,相当于精简版的SQL Express. 二.使用 VS版本为2015,默认安装位置为:C:\Program Files\Microsoft ...