4-1 Strongly Connected Components (17分)
来源:互联网 发布:C语言迭代法求立方根 编辑:程序博客网 时间:2024/05/16 15:10
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 intoStronglyConnectedComponents
to handle (print with a certain format) each vertex that is visited. The functionStronglyConnectedComponents
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 50 11 22 03 13 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.
#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 */PtrToVNode insert(PtrToVNode p,int x) { p->Next=(PtrToVNode)malloc(sizeof(struct VNode)); p=p->Next; p->Vert=x; p->Next=NULL; return p; } Graph ReadG(){int i,j,m,n,x,y; scanf("%d%d",&m,&n); Graph p=(Graph)malloc(sizeof(struct GNode)); PtrToVNode *rear=(PtrToVNode*)malloc(m*sizeof(struct VNode)); p->Array=(PtrToVNode*)malloc(m*sizeof(struct VNode)); p->NumOfVertices=m; p->NumOfEdges=n; for(i=0;i<m;i++){ rear[i]=p->Array[i]=(PtrToVNode)malloc(sizeof(struct VNode)); p->Array[i]->Next=NULL; } for(i=0;i<n;i++){ scanf("%d%d",&x,&y); rear[x]=insert(rear[x],y); } for(i=0;i<m;i++){ PtrToVNode tmp=p->Array[i]; p->Array[i]=p->Array[i]->Next; tmp->Next=NULL; free(tmp); } return p; }int index=0;int top=0;int Stack[MaxVertices];int DFN[MaxVertices];int LOW[MaxVertices];int vi[MaxVertices];int whether[MaxVertices];int min(int x, int y){if( x >= y ){return y;}else if( x < y ){return x;}}void Tarjan(int u, Graph G){ int t, v; DFN[u]=LOW[u]=index++; Stack[top++]=u; vi[u]=1; struct VNode *temp; temp=G->Array[u]; while(temp!=NULL) { v=temp->Vert; if(DFN[v]==-1) { Tarjan(v, G); LOW[u]=min(LOW[u],LOW[v]);}else if(whether[v]==0){LOW[u]=min(LOW[u],DFN[v]);}temp=temp->Next;}if(DFN[u]==LOW[u]){ do { t=Stack[--top]; printf("%d ", t); whether[t]=1; }while(u!=t); printf("\n"); }}void StronglyConnectedComponents( Graph G, void (*visit)(Vertex V) ){for(int i=0; i<G->NumOfVertices; i++){vi[i]=0;whether[i]=0;DFN[i]=LOW[i]=-1;}for(int i=0; i<G->NumOfVertices; i++){if(vi[i]==0){Tarjan(i, G);}}}贵校PTA作业,请自行绕道,以免不必要的麻烦
1 0
- 4-1 Strongly Connected Components (17分)
- 4-1 Strongly Connected Components (17分)/PTA
- 4-1 Strongly Connected Components
- Strongly Connected Components
- c编写Strongly Connected Components
- Programming Question-4-Strongly Connected Components Problem through DFS
- POJ 2186 -- Popular Cows (Strongly Connected Components)
- CF 427C. Checkposts Strongly connected components
- Stanford - Algorithms: Design and Analysis, Part 1 - Week 4 Assignment: strongly connected component
- HDOJ4635 strongly connected
- HDOJ 4635: Strongly connected
- HDU4635 Strongly connected
- hdu 4635Strongly connected
- cf475B Strongly Connected City
- HDU 4635 Strongly connected
- D - Strongly Connected City
- HDU 4635 Strongly connected
- HDU4635 Strongly connected
- 二叉树的建立,遍历和深度检测
- oracle数据库自带dual浅解
- 基于quartz的调度系统实现
- failed to read artifact descriptor for xx
- java开发微信公众平台-- 服务器配置
- 4-1 Strongly Connected Components (17分)
- Java从数据库中读取Blob对象图片并显示
- 活动被回收时的处理
- 音乐格式问题
- 过滤器(Filter)应用:全站压缩----Gzip网页压缩输出
- 练习——HashMap
- Java之jdbc学习
- java登录验证码 用到spring框架
- Linux 关闭和重启的命令