For a given undirected graph with N vertices and E edges, please list all the connected components by both DFS and BFS. Assume that all the vertices are numbered from 0 to N-1. While searching, assume that we always start from the vertex with the smallest index, and visit its adjacent vertices in ascending order of their indices.

Input Specification:

Each input file contains one test case. For each case, the first line gives two integers N (0<N<=10) and E, which are the number of vertices and the number of edges, respectively. Then E lines follow, each described an edge by giving the two ends. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in each line a connected component in the format "{ v1 v2 ... vk }". First print the result obtained by DFS, then by BFS.

Sample Input:

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

Sample Output:

{ 0 1 4 2 7 }
{ 3 5 }
{ 6 }
{ 0 1 2 7 4 }
{ 3 5 }
{ 6 }

这题比较水……就是写个图的DFS和BFS……当然DFS是遍历到这个点才标记该点已经被访问,并且已经访问过的点就不要再去访问了,不然如果图中有环的话就一直递归下去了,BFS的话是只要入队就把相应节点标记(不用管它有没有遍历到,因为只要入队肯定会遍历到),这样标记过的点就不用再入队了,从而避免了重复入队的发生。

下面是代码:

//
//  main.c
//  List Components
//
//  Created by 余南龙 on 2016/12/6.
//  Copyright © 2016年 余南龙. All rights reserved.
//

#include <stdio.h>
#include <string.h>

#define MAXV 10000
int Graph[MAXV][MAXV];
int visit[MAXV], connected[MAXV];
int N, E, top;

void DFS(int v){
    int i;

    connected[++top] = v;
    visit[v] = ;
    ; i < N; i++){
         == Graph[v][i]&& == visit[i]){
            DFS(i);
        }
    }
}

void BFS(int v){
    ];
    , j = , i;

    Q[++tail] = v;
    visit[Q[j]] = ;
    ){
        connected[++top] = Q[j];
        ; i < N; i++){
             == Graph[Q[j]][i]&& == visit[i]){
                Q[++tail] = i;
                visit[i] = ;
            }
        }
        j++;
        if(tail < j){
            break;
        }
    }
}

void Init(){
    int i, u, v;

    scanf("%d%d", &N, &E);
    ; i < E; i++){
        scanf("%d%d", &u, &v);
        Graph[u][v] = Graph[v][u] = ;
    }
}

void Output(){
    int i;

    printf("{ ");
    ; i <= top; i++){
        printf("%d ", connected[i]);
    }
    printf("}\n");
}

int main(){
    int j;
    Init();
    memset(visit, , MAXV * sizeof(int));
    top = -;
    ; j < N; j++){
         == visit[j]){
            DFS(j);
            Output();
            top = -;
        }
    }
    memset(visit, , MAXV * sizeof(int));
    top = -;
    ; j < N; j++){
         == visit[j]){
            BFS(j);
            Output();
            top = -;
        }
    }
    ;
}

PTA List Components的更多相关文章

  1. PTA Strongly Connected Components

    Write a program to find the strongly connected components in a digraph. Format of functions: void St ...

  2. 浙大PTA - - File Transfer

    题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21732 #include "iostream" #includ ...

  3. pta 编程题13 File Transfer

    其它pta数据结构编程题请参见:pta 这道题考察的是union-find并查集. 开始把数组中每个元素初始化为-1,代表没有父节点.为了使树更加平衡,可以让每一个连通分量的树根的负值代表这个连通分量 ...

  4. PTA 05-树8 File Transfer (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/670 5-8 File Transfer   (25分) We have a netwo ...

  5. angular2系列教程(三)components

    今天,我们要讲的是angualr2的components. 例子

  6. 【shadow dom入UI】web components思想如何应用于实际项目

    回顾 经过昨天的优化处理([前端优化之拆分CSS]前端三剑客的分分合合),我们在UI一块做了几个关键动作: ① CSS入UI ② CSS作为组件的一个节点而存在,并且会被“格式化”,即选择器带id前缀 ...

  7. [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), ...

  8. Web Components初探

    本文来自 mweb.baidu.com 做最好的无线WEB研发团队 是随着 Web 应用不断丰富,过度分离的设计也会带来可重用性上的问题.于是各家显神通,各种 UI 组件工具库层出不穷,煞有八仙过海之 ...

  9. [备忘] Automatically reset Windows Update components

    这两天遇到Windows 10的更新问题,官方有一个小工具,可以用来修复Windows Update的问题,备忘如下 https://support.microsoft.com/en-us/kb/97 ...

随机推荐

  1. python数据分析之pandas库的DataFrame应用二

    本节介绍Series和DataFrame中的数据的基本手段 重新索引 pandas对象的一个重要方法就是reindex,作用是创建一个适应新索引的新对象 ''' Created on 2016-8-1 ...

  2. [MySql] - 开启外部访问

    打开 mysql 的查询窗口(使用root),使用SQL: -- 使用mysql库 use mysql; -- 更新密码 update user set password=PASSWORD('xxxx ...

  3. ruby中rsa加签解签方法

    # coding:utf-8require 'openssl'require 'base64'# rsa签名,文本内容和私钥路径def rsa_sign(data,private_key_path) ...

  4. activiti自定义流程之整合(七):完成我的申请任务

    在上一篇的获得我的申请中,可以看到js代码中还包含了预览和完成任务的代码,既然上一篇已经罗列了相关代码,这里也就不重复. 那么需要补充的是,在上边的完成任务的js代码中,我们还调用了getTaskFo ...

  5. 【转】CSS z-index 属性的使用方法和层级树的概念

    文章转自:CSS z-index 属性的使用方法和层级树的概念,另外加了一点自己的注释 CSS 中的 z-index 属性用于设置节点的堆叠顺序, 拥有更高堆叠顺序的节点将显示在堆叠顺序较低的节点前面 ...

  6. [Tex学习笔记]积分平均

    $\def\avint{\mathop{\mathchoice{\,\rlap{-}\!\!\int} {\rlap{\raise.15em{\scriptstyle -}}\kern-.2em\in ...

  7. Python中利用LSTM模型进行时间序列预测分析

    时间序列模型 时间序列预测分析就是利用过去一段时间内某事件时间的特征来预测未来一段时间内该事件的特征.这是一类相对比较复杂的预测建模问题,和回归分析模型的预测不同,时间序列模型是依赖于事件发生的先后顺 ...

  8. Mysql备份迁移——MySqlBackup(.net)——(无法解决视图嵌视图报错)

    这里是利用MySqlBackup,可以再nuget中下载. 无法解决视图嵌视图报错的问题,只导表跟数据比较合适,如果有视图嵌视图,请参照Mysql备份迁移——Mysqldump(.NET调用Mysql ...

  9. 安卓:assets目录下的文本文件(不受R文件节制)

    try { InputStream in = getAssets().open("testAsset.txt"); byte[] buffer = new byte[1024]; ...

  10. Consuming a Web Service in AX 2012

    Consuming a Web Service in AX 2012 在AX2012版本中如果想调用外部的Web Service变得非常容易. 第一步,在VS中创建一个Web Service并发布 第 ...