PTA List Components

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] = 1;
    for(i = 0; i < N; i++){
        if(1 == Graph[v][i]&&0 == visit[i]){
            DFS(i);
        }
    }
}

void BFS(int v){
    int Q[MAXV + 1];
    int tail = -1, j = 0, i;

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

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

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

void Output(){
    int i;

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

int main(){
    int j;
    Init();
    memset(visit, 0, MAXV * sizeof(int));
    top = -1;
    for(j = 0; j < N; j++){
        if(0 == visit[j]){
            DFS(j);
            Output();
            top = -1;
        }
    }
    memset(visit, 0, MAXV * sizeof(int));
    top = -1;
    for(j = 0; j < N; j++){
        if(0 == visit[j]){
            BFS(j);
            Output();
            top = -1;
        }
    }
    return 0;
}
时间: 2025-01-05 21:20:25

PTA List Components的相关文章

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

PTA 5-8(English) File Transfer (25) - 并查集 - 数组实现

题目:http://pta.patest.cn/pta/test/16/exam/4/question/670 PTA - Data Structures and Algorithms (English) - 5-8 We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer t

pta 编程题13 File Transfer

其它pta数据结构编程题请参见:pta 这道题考察的是union-find并查集. 开始把数组中每个元素初始化为-1,代表没有父节点.为了使树更加平衡,可以让每一个连通分量的树根的负值代表这个连通分量包含的节点数,然后在union时把小的树并到大的树上. 另外在find操作时可以用递归的方式使查找路径上的所有节点的父节点都改为根节点,以实现路径压缩,在后续查找过程中会更快. 1 #include <iostream> 2 #include <vector> 3 using name

App 组件化/模块化之路——Android 框架组件(Android Architecture Components)使用指南

面对越来越复杂的 App 需求,Google 官方发布了Android 框架组件库(Android Architecture Components ).为开发者更好的开发 App 提供了非常好的样本.这个框架里的组件是配合 Android 组件生命周期的,所以它能够很好的规避组件生命周期管理的问题.今天我们就来看看这个库的使用. 通用的框架准则 官方建议在架构 App 的时候遵循以下两个准则: 关注分离 其中早期开发 App 最常见的做法是在 Activity 或者 Fragment 中写了大量

Android Studio中的App Components重要点记述

阅读英文文档而理解的file:///E:/Android2016/sdk/docs/guide/components/fundamentals.html#Components App Compnents 每个component都是系统可以进入你的app的一种方式,但是不是所有的component都是对于user而言的真实的entry points. 共有四种app components: Activity: 每一个activity代表用户界面的一个单独的屏幕,这些activity是相互独立的 S

ERROR&lt;53761&gt; - Plugins - conn=-1 op=-1 msgId=-1 - Connection Bind through PTA failed (91). Retrying...

LDAP6.3在DSCC控制台启动实例完成,但是操作状态显示“意外错误”,查看日志如下: 04/May/2016:21:10:39 +0800] - Sun-Java(tm)-System-Directory/6.3 B2008.0311.0224 (32-bit) starting up[04/May/2016:21:10:39 +0800] - Listening on all interfaces port 11111 for LDAP requests[04/May/2016:21:10

Building Maintainable Software-java篇之Keep Architecture Components Balanced

Building encapsulation boundaries is a crucial skill in software architecture. -George H. Fairbanks in Just Enough Architecture Guideline: ? Balance the number and relative size of top-level components in your code. ? Do this by organizing source cod

PyQt5 Introduction and components

在开始写代码以前,对PyQt5整体大致了解一下还是有必要的.这方面的东西看看PyQt5官方给出的文档就好,下面就是我从文中截取的部分内容: Introduction PyQt5 is a set of Python bindings for v5 of the Qt application framework from The Qt Company. Qt is a set of C++ libraries and development tools that includes platform

Method and apparatus for verification of coherence for shared cache components in a system verification environment

A method and apparatus for verification of coherence for shared cache components in a system verification environment are provided. With the method and apparatus, stores to the cache are applied to a cache functional simulator in the order that they