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 {
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的更多相关文章
- Strongly connected components
拓扑排列可以指明除了循环以外的所有指向,当反过来还有路可以走的话,说明有刚刚没算的循环路线,所以反过来能形成的所有树都是循环
- algorithm@ Strongly Connected Component
Strongly Connected Components A directed graph is strongly connected if there is a path between all ...
- [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), ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- [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 ...
- [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 ...
- cf475B Strongly Connected City
B. Strongly Connected City time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Strongly connected(hdu4635(强连通分量))
/* http://acm.hdu.edu.cn/showproblem.php?pid=4635 Strongly connected Time Limit: 2000/1000 MS (Java/ ...
- [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), ...
随机推荐
- 安装Nginx作为Windows服务自启动运行
如果Nginx每次使用都需要手动启动确实很麻烦,所以最好将其设置为Windows系统服务,开机自启动就行了. 1.下载并修改运行环境支持程序 1).下载地址:http://ng-srvinst.att ...
- webpack 往右一点之 “你好,初次见面”
webpack 模块打包器 模块化工具的目标: 将依赖树拆分成按需加载的块 初始化加载的耗时尽量少 各种静态资源都可以视作模块 将第三方库整合成模块 自定义打包逻辑 适合大项目
- 5、IMS网元
1.会话管理和路由类(call session control function,呼叫会话控制功能) (1)代理呼叫会话控制功能P-CSCF 是IMS中与用户的第一个连接点,提供”代理(proxy)“ ...
- java面向对象基础
1.对象:用来描述客观事物的一个实体,由一组属性和方法构成. 属性:对象具有的各种特征.(成员变量) 方法:对象执行的操作. 2.类:一组具有相同属性和方法的对象的一个归纳类型.对象是类的一个具体表现 ...
- 软件测试之loadrunner学习笔记-02集合点
loadrunner学习笔记-02集合点 集合点函数可以帮助我们生成有效可控的并发操作.虽然在Controller中多用户负载的Vuser是一起开始运行脚本的,但是由于计算机的串行处理机制,脚本的运行 ...
- 创建DAO模式的步骤
1.建立数据库epet 2.创建实体类,和相对应的数据库是对应的 3.创建Dao的基类接口类BaseDao 4.创建Dao的实现类BaseDaoImpl 5.创建具体表的Dao类 6.创建具体表的Da ...
- javascrip小笔记
function getCookie(name) {//获取name为 var arr, reg = new RegExp("(^| )" + name + "=([^; ...
- JVM 内存的那些事
转自:http://blog.jobbole.com/104863/ 对于Java程序员你来说,在虚拟机内存管理的帮助下,不需要为每个new对象都匹配free操作,内存泄露和内存溢出等问题也不太容易出 ...
- Spark Streaming源码解读之No Receivers彻底思考
本期内容 : Direct Acess Kafka Spark Streaming接收数据现在支持的两种方式: 01. Receiver的方式来接收数据,及输入数据的控制 02. No Receive ...
- 从入门到精通C++需要学的10本书
学习C++从入门到精通的的十本最经典书籍 文章来源中国IT实验室收集整理作者佚名更新时间2009-5-16 12:27:05 保存本文保存本文推荐给好友推荐给好友收藏本页收藏本页 欢迎进入C/C++ ...