zoukankan      html  css  js  c++  java
  • 列出联通集

    程序自己测试没问题,为什么通过PTA呢?悲伤大哭

    06-图1 列出连通集   (25分)

    给定一个有NN个顶点和EE条边的无向图,请用DFS和BFS分别列出其所有的连通集。假设顶点从0到N-1N1编号。进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点。

    输入格式:

    输入第1行给出2个整数NN(0<Nle 100<N10)和EE,分别是图的顶点数和边数。随后EE行,每行给出一条边的两个端点。每行中的数字之间用1空格分隔。

    输出格式:

    按照"{ v_1v1 v_2v2 ... v_kvk }"的格式,每行输出一个连通集。先输出DFS的结果,再输出BFS的结果。

    输入样例:

    8 6
    0 7
    0 1
    2 0
    4 1
    2 4
    3 5
    

    输出样例:

    { 0 1 4 2 7 }
    { 3 5 }
    { 6 }
    { 0 1 2 7 4 }
    { 3 5 }
    { 6 }
    代码如下:
    #include <stdio.h>  
    #include <string.h>  
    #include <stdlib.h>  
    #define MAXN 10  
      
    int visit[MAXN]; 
    
    typedef struct Node *PtrToNode; 
    typedef PtrToNode Position;
    typedef struct QNode *Queue;  
    struct Node { /* 队列中的结点 */  
        int Data;  
        PtrToNode Next;  
    };  
      
       
    struct QNode {  
        Position Front, Rear;  /* 队列的头、尾指针 */  
    };  
      
    
    typedef struct ENode* Edge;  
    struct ENode {  
       int V1, V2;  
    };  
      
    typedef struct AdjNode* PtrA;  
    struct AdjNode {  
        int V;  
        PtrA Next;  
    };  
      
    typedef struct VNode {  
        PtrA FirstEdge;  
    }AdjList[MAXN];  
      
    typedef struct GNode *Graph;  
    struct GNode {  
        int Nv, Ne;  
        AdjList GList;  
    };  
      
    Graph CreateGraph(int n) {  
        int v;  
        Graph G = (Graph)malloc(sizeof(struct GNode));  
        G->Nv = n;  
        G->Ne = 0;  
        for(v = 0; v < n; v++)  
            G->GList[v].FirstEdge = NULL;  
        return G;  
    }  
      
    void InsertEdge(Graph G, Edge E) {  
        PtrA newnode = (PtrA)malloc(sizeof(struct AdjNode));  
        newnode->V = E->V2;  
        newnode->Next = G->GList[E->V1].FirstEdge;  
        G->GList[E->V1].FirstEdge = newnode;  
        newnode = (PtrA)malloc(sizeof(struct AdjNode));  
        newnode->V = E->V1;  
        newnode->Next = G->GList[E->V2].FirstEdge;  
        G->GList[E->V2].FirstEdge = newnode;  
    }  
        
      
    void DFS(Graph G, int V) {  
        PtrA W;  
        printf(" %d",V);
    	visit[V]=1;  
        for(W = G->GList[V].FirstEdge; W; W = W->Next)  
            if(!visit[W->V])  
                DFS(G, W->V);  
    };  
    int DeleteQ( Queue Q )  
    {  /*出队列*/   
        Position FrontCell;   
        int m;  
         FrontCell = Q->Front;  
            if ( Q->Front == Q->Rear ) /* 若队列只有一个元素 */  
                Q->Front = Q->Rear = NULL; /* 删除后队列置为空 */  
            else                       
                Q->Front = Q->Front->Next;  
            m = FrontCell->Data;  
            free( FrontCell );  /* 释放被删除结点空间  */  
            return m;  
    }
    Queue  CreatQueue()  
    {     
       /*建立一个新的队列*/   
        Queue Q;  
        Q=(Queue)malloc(sizeof(struct QNode));  
        Q->Front=NULL;  
        Q->Rear=NULL;  
        return Q;  
    }  
    void AddQ( Queue Q ,int m)  
    {  
      /*入队列*/   
        PtrToNode que;  
        que=(PtrToNode)malloc(sizeof (struct Node));  
        que->Data=m;  
        que->Next=NULL;    
        if (Q->Front==NULL){   
             Q->Front=que;  
             Q->Rear=que;  
           }  
         else {   
           Q->Rear->Next=que;  
           Q->Rear=que;   
           }  
    }  
    int IsEmpty(Queue Q)  
    {  
       /*判断队首或队尾任一个指针是否为空即可*/  
       if(Q->Front==NULL)  
         return 1;  
       else  
        return 0;  
    } 
    void BFS(Graph G,int V)
    {
        PtrA W;
        int T,ss; 
    	Queue Q; 
    	Q=CreatQueue(); 
        printf(" %d",V);
    	visit[V]=1;
    	AddQ(Q,V);
    	while(!IsEmpty(Q)){
    	   T=DeleteQ(Q); 
        for(W = G->GList[T].FirstEdge; W; W = W->Next)
         	{  
               ss=W->V; 
            if(!visit[ss])  { 
                printf("%2d",ss);
    			visit[ss]=1; 
    			AddQ(Q,ss);
    	       }
    	   }
       }
    }
    int main() {  
        int n, i;  
        Graph G;  
        Edge E;  
        memset(visit, 0, sizeof(visit));  
        scanf("%d", &n);  
        G = CreateGraph(n);  
        scanf("%d", &G->Ne);  
        if(G->Ne) {  
            E = (Edge)malloc(sizeof(struct ENode));  
            for(i = 0; i < G->Ne; i++) {  
                scanf("%d%d", &E->V1, &E->V2);  
                InsertEdge(G, E);  
            }  
        }
     
    	for(i=0;i<n;i++) 
    	{ 
    	    
    	    if(!visit[i])
    	    { 
    		printf("{");  
               DFS(G, i); 
               printf(" }");  
    		  printf("
    "); 
    	}
    }
      for(i=0;i<MAXN;i++)
       visit[i]=0;
    	for(i=0;i<n;i++) 
    	{ 
    	    
    	    if(!visit[i])
    	    { 
    		printf("{");  
               BFS(G, i); 
               printf(" }");  
    		  printf("
    "); 
    	}
    }
        return 0;  
    }

    运行结果:
    
    
    
    
  • 相关阅读:
    tail命令和head命令查询文件指定行数
    php unserialize(): Error at offset 470 of 660 bytes
    屏蔽搜索引擎收录robots.txt文件下载
    php-apc为magento加速
    magento导入csv文件到数据库中乱码
    magento 上传csv表格中实例化对象例子
    magento上新产品,前台不显示不显示属性,后台却有属性问题
    php裁剪图片
    概念:静态static相关知识
    概念 : 重定向概念
  • 原文地址:https://www.cnblogs.com/jacksin/p/8830219.html
Copyright © 2011-2022 走看看