6-10 Strongly Connected Components (30分)

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.

找图的强连通分量,题目中的建图的函数没给出定义,实际使用邻接表。

由于边数很少可以考虑先求所有边可达矩阵,mp[i][j]为1,表示存在i到j的路径,可以用邻接矩阵的n次方求得。然后排着判断即可,一个强连通分量里的点必定都是相互可达的。

代码:

#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 */
    int a,b;
    Graph G = (Graph)malloc(sizeof(GNode));
    scanf("%d%d",&G -> NumOfVertices,&G -> NumOfEdges);
    G -> Array = (PtrToVNode *)malloc(sizeof(PtrToVNode) * G -> NumOfVertices);
    for(int i = 0;i < G -> NumOfVertices;i ++) {
        G -> Array[i] = NULL;
    }
    for(int i = 0;i < G -> NumOfEdges;i ++) {
        scanf("%d%d",&a,&b);
        PtrToVNode p = (PtrToVNode)malloc(sizeof(VNode));
        p -> Vert = b;
        p -> Next = G -> Array[a];
        G -> Array[a] = p;
    }
    return G;
}

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 */
void StronglyConnectedComponents( Graph G, void (*visit)(Vertex V) ) {
    int mp[MaxVertices][MaxVertices] = {0},num = G -> NumOfVertices;
    int vis[MaxVertices] = {0};
    for(int i = 0;i < num;i ++) {
        PtrToVNode p = G -> Array[i];
        while(p) {
            mp[i][p -> Vert] = 1;
            p = p -> Next;
        }
    }
    for(int i = 0;i < num;i ++) {
        for(int j = 0;j < num;j ++) {
            for(int k = 0;k < num;k ++) {
                if(mp[i][k] && mp[k][j]) mp[i][j] = 1;
            }
        }
    }
    for(int i = 0;i < num;i ++) {
        if(vis[i]) continue;
        visit(i);
        vis[i] = 1;
        for(int j = 0;j < num;j ++) {
            if(!vis[j] && mp[i][j] && mp[j][i]) {
                vis[j] = 1;
                visit(j);
            }
        }
        putchar(‘\n‘);
    }
}

原文地址:https://www.cnblogs.com/8023spz/p/12252266.html

时间: 2024-10-10 19:41:33

6-10 Strongly Connected Components (30分)的相关文章

PTA Strongly Connected Components

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

第5章-10.两数之和 (30分)(列表实现和字典实现)

给定一组整数,还有一个目标数,在给定这组整数中找到两个数字,使其和为目标数,如找到,解是唯一的.找不到则显示 "no answer".输出的下标按从小到大排序.用一重循环加字典实现. 输入格式: 在一行中给出这组数. 在下一行输入目标数 输出格式: 在一行中输出这两个数的下标,用一个空格分开. 输入样例1: 在这里给出一组输入.例如: 2,7,11,15 9 输出样例1: 在这里给出相应的输出.例如: 0 1 输入样例2: 在这里给出一组输入.例如: 3,6,9 10 输出样例2: 在

Strongly connected components

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

[email&#160;protected] Strongly Connected Component

Strongly Connected Components A directed graph is strongly connected if there is a path between all pairs of vertices. A strongly connected component (SCC) of a directed graph is a maximal strongly connected subgraph. For example, there are 3 SCCs in

Strongly connected

hdu4635:http://acm.hdu.edu.cn/showproblem.php?pid=4635 题意:给你一个有向图,然后问你最多可以加多少条边,是的原图不是一个强连通图. 题解:这一题确实不会,图论做的太少了,一下是一个人分析,觉得分析的很不错,代码也是看别人的. 首先强连通缩点,缩点之后,最终添加完边的图,肯定可以分成两个部X和Y,其中只有X到Y的边没有Y到X的边;  *那么要使得边数尽可能的多,则X部肯定是一个完全图,Y部也是,同时X部中每个点到Y部的每个点都有一条边;  *

Codeforces 292D Connected Components (并查集)

Codeforces 292D Connected Components (并查集) 题意 给出一张无向图,每次询问删去第Li--Ri 条边 求此时有多少个连通块 题解 求出一个前缀 Li 表示 加入前 i 条边时图的连通状况 以及一个后缀 Ri 表示 加入后 i 条边时图的连通状况 对于每个询问 删除 s--t 条边 只要将 L s-1 和 R t+1 合并 一下 就行了 合并 其实 就是讲 s-1 和 t+1 对应的 f[ i ] Union 一下就行了 为什么 这就相当于把前缀 i 和 后

[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), write a function to find the number of connected components in an undirected graph. Example 1: 0          3 |          | 1 --- 2    4 Given n = 5 and

pta08-图7 公路村村通 (30分)

08-图7 公路村村通   (30分) 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本. 输入格式: 输入数据包括城镇数目正整数N(≤1000)和候选道路数目M(≤3N):随后的M行对应M条道路,每行给出3个正整数,分别是该条道路直接连通的两个城镇的编号以及该道路改建的预算成本.为简单起见,城镇从1到N编号. 输出格式: 输出村村通需要的最低成本.如果输入数据不足以保证畅通,则输出−1,表示需要建设更多公路. 输入样例: 6

PTA 07-图5 Saving James Bond - Hard Version (30分)

07-图5 Saving James Bond - Hard Version   (30分) This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was captured by a group of drug dealers. He was sent to a small piece of lan