PAT 05-1 List Components (简单DFS与BFS)

刚一拿到这道题把他想的太复杂了

明明是长度最大为十的顺序结构就能解决的问题,竟然优先想到用链表。

BFS牵扯到一个队列的操作,在这种小规模数据里面 用顺序结构好很多

题目如下:

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 }

题目链接:点我点我点我

代码如下:

# include <stdio.h>
# include <stdlib.h>
void DFS(int i);
void BFS(int i);

int vertice[10][10];
int flag[10];
int n,e; 

int main()
{
    int i,j,k;
    scanf("%d%d",&n,&e);
    int a,b;
    for (i=0;i<e;i++)
    {
        scanf("%d%d",&a,&b);
        vertice[a][b] = vertice[b][a] = 1;
    }
    for (i=0;i<n;i++)
       {
        if (flag[i]==1) continue;
        else  printf("{ "),DFS(i),printf("}\n");
       }
    for(i=0;i<10;i++)
          flag[i] = 0;
    for (i=0;i<n;i++)
       {
        if (flag[i]==1) continue;
        else printf("{ "),BFS(i),printf("}\n");
       }
}

void DFS(int i)
{
    printf("%d ",i),flag[i] = 1;
    int j;
    for (j=0;j<n;j++)
        if (vertice[i][j]==1&&flag[j]==0)
          {
             DFS(j);
          }
}

void BFS(int i)
{
   int start,end,Q[100];
   start = end = 0;
   Q[end++] = i;flag[i] = 1;
   int temp,j;
   while (start<end)
   {
       temp = Q[start++];
       printf("%d ",temp);
       for (j=0;j<n;j++)
          if (vertice[temp][j]==1&&flag[j]==0)
          {
              Q[end++] = j;
              flag[j] = 1;
          }
   }
}
时间: 2024-10-03 19:15:54

PAT 05-1 List Components (简单DFS与BFS)的相关文章

hdu 1016 Prime Ring Problem (简单DFS)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25700    Accepted Submission(s): 11453 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb

Red and Black(简单dfs)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12519    Accepted Submission(s): 7753 Problem Description There is a rectangular room, covered with square tiles. Each tile is color

ZOJ2165 简单DFS搜索

1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<algorithm> 5 #define MAXN 25 6 using namespace std; 7 int h,w; 8 int ans; 9 int dir[4][2]={-1,0,1,0,0,-1,0,1}; 10 char map[MAXN][MAXN]; 11 12 bool ok(int x,int

hdu 4739Zhuge Liang&#39;s Mines(简单dfs,需要注意重点)

Zhuge Liang's Mines Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1166    Accepted Submission(s): 505 Problem Description In the ancient three kingdom period, Zhuge Liang was the most famous

poj2386 Lake Counting(简单DFS)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1562 ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢

POJ 1979 Red and Black (简单dfs)

题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define INF 2147483647 int w,h; char a[22][22]; int dir[4][2] = {-1,0,1,0,0,-1,0,1}; int ans = 0; void dfs(int x,int y){ if(x < 0 || x >= h || y < 0 || y &g

lightoj 1111 - Best Picnic Ever(dfs or bfs)

题目链接 http://www.lightoj.com/volume_showproblem.php?problem=1111 题意:给你一个有向图再给你几个人的位置,问所有人可以在哪些点相聚. 简单的搜索题,可以用bfs也可以用dfs,要注意的是存边的时候最好用vector,因为边比较少. 用struct会超时.下面附上dfs和bfs代码. #include <iostream> #include <cstring> #include <queue> #include

关于DFS和BFS的理解 以及坐标的定义

1: 坐标类型搜索 :这种类型的搜索题目通常来说简单的比较简单,复杂的通常在边界的处理和情况的讨论方面会比较复杂,分析这类问题,我们首先要抓住题目的意思,看具体是怎么建立坐标系(特别重要), 然后仔细分析到搜索的每一个阶段是如何通过条件转移到下一个阶段的.确定每一次递归(对于DFS)的回溯和深入条件,对于BFS,要注意每一次入队的条件同时注意判重.要牢牢把握 目标状态是一个什么状态,在什么时候结束搜索.还有,DFS过程的参数如何设定,是带参数还是不带参数,带的话各个参数一定要保证能完全的表示一个

图的DFS与BFS遍历

一.图的基本概念 1.邻接点:对于无向图无v1 与v2之间有一条弧,则称v1与v2互为邻接点:对于有向图而言<v1,v2>代表有一条从v1到v2的弧,则称v2为v1的邻接点. 2.度:就是与该顶点相互关联的弧的个数. 3.连通图:无向图的每个顶点之间都有可达路径,则称该无向图为连通图.有向图每个顶点之间都有<v1,v2>和<v2,v1>,则称此有向图为强连通图. 二.存储结构 1.邻接矩阵存储(Adjacency Matrix) 对无权图,顶点之间有弧标1,无弧标0: