dfs代码:

#include<iostream>
#include<Algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
int cnt,m;
int head[50005];
bool s[50005]={0};
struct node
{
    int w;
    int to;
    int next;
}edge[50005];
void add(int u,int v,int w)
{
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    edge[cnt].to=v;
    head[u]=cnt++;
}
void dfs(int x)
{
    s[x] = true;
    printf("%d\n", x);
    for(int i= head[x];i!=-1;i=edge[i].next)//刚开始不太理解这里,其实就是循环一个点(比如u)的所有边(u-v,u-v1...)。。。递归是递归v的所有边。
    {
        if(!s[edge[i].to])
        {

dfs(edge[i].to);
        }
    }
}
int main()
{
    int n,x,y,w;
    while(cin>>n)
    {
        memset(head,-1,sizeof(head));
        for(int i=1;i<=n;i++)
        {
            cin>>x>>y;
            add(x,y,0);
        }
        dfs(1);
    }
        return 0;
}

/*1 2
2 3
3 4
1 3
4 1
1 5
4 5
edge[0].to=2;edge[0].next=-1;head[1]=0;
edge[1].to=3;edge[1].next=-1;head[2]=1;
edge[2].to=4;edge[2].next=-1;head[3]=2;
edge[3].to=3;edge[3].next= 0;head[1]=3;
edge[4].to=1;edge[4].next=-1;head[4]=4;
edge[5].to=5;edge[5].next= 3;head[1]=5;
edge[6].to=5;edge[6].next= 4;head[4]=6;*/

bfs代码:

#include<bits/stdc++.h>
#define maxn 1000005
using namespace std;
struct node
{
    int to;
    int next;
    int w;
}edge[maxn];
int cnt;
int head[maxn];
bool s[maxn];
int que[maxn];
void add(int u,int v,int w)
{
    edge[cnt].to=v;
    edge[cnt].next=head[u];
    edge[cnt].w=w;
    head[u]=cnt++;
}
void bfs(int x)
{
    int i,j;
    int iq=0;
    que[iq++]=x;
    s[x]=true;
    for(i=0;i<iq;i++)
    {
        cout<<que[i]<<" ";
        for(j=head[que[i]];j!=-1;j=edge[j].next)
        {
            if(!s[edge[j].to])
            {
                que[iq++]=edge[j].to;
                s[edge[j].to]=true;
            }
        }
    }
}
int main()
{
    int n;
    int x,y;
    memset(head,-1,sizeof(head));
    while(cin>>n)
    {
        while(n--)
        {
        cin>>x>>y;
        add(x,y,0);
        }
        bfs(1);
    }
    return 0;
}

dfs和bfs(链式前向星实现)的更多相关文章

  1. 链式前向星存树图和遍历它的两种方法【dfs、bfs】

    目录 一.链式前向星存图 二.两种遍历方法 一.链式前向星存图:(n个点,n-1条边) 链式前向星把上面的树图存下来,输入: 9 ///代表要存进去n个点 1 2 ///下面是n-1条边,每条边连接两 ...

  2. 链式前向星写法下的DFS和BFS

    Input 5 7 1 2 2 3 3 4 1 3 4 1 1 5 4 5 output 1 5 3 4 2 #include<bits/stdc++.h> using namespace ...

  3. poj-1459-最大流dinic+链式前向星-isap+bfs+stack

    title: poj-1459-最大流dinic+链式前向星-isap+bfs+stack date: 2018-11-22 20:57:54 tags: acm 刷题 categories: ACM ...

  4. Pants On Fire(链式前向星存图、dfs)

    Pants On Fire 传送门:链接  来源:upc9653 题目描述 Donald and Mike are the leaders of the free world and haven't ...

  5. zzuli 2130: hipercijevi 链式前向星+BFS+输入输出外挂

    2130: hipercijevi Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 595  Solved: 112 SubmitStatusWeb B ...

  6. 链式前向星BFS

    本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5399068 采用链式前向星的BFS: #include <iostream> #incl ...

  7. 链式前向星DFS

    本文链接:http://www.cnblogs.com/Ash-ly/p/5399057.html 采用链式前向星存图的DFS: #include <iostream> #include ...

  8. zzuli 2131 Can Win dinic+链式前向星(难点:抽象出网络模型+建边)

    2131: Can Win Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 431  Solved: 50 SubmitStatusWeb Board ...

  9. 网络流dinic模板,邻接矩阵+链式前向星

    //这个是邻接矩阵的#include<iostream> #include<queue> #include<string.h> #include<stdio. ...

  10. 最短路 spfa 算法 && 链式前向星存图

    推荐博客  https://i.cnblogs.com/EditPosts.aspx?opt=1 http://blog.csdn.net/mcdonnell_douglas/article/deta ...

随机推荐

  1. linux中断源码分析 - 概述(一)

    本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 关于中断和异常 一般在书中都会把中断和异常一起说明,因为它们具有相同的特点,同时也有不同的地方.在CPU里,中断 ...

  2. session和cookie知识点总结

    cookie小结:1.cookie是在服务端创建2.cooki是保存在浏览器这一端3.cookie的生命周期可以通过 cookie.setMaxAge(2000);(如果不设置生命周期,cookie的 ...

  3. 一篇自己都看不懂的Matrix tree总结

    Matrix tree定理用于连通图生成树计数,由于博主太菜看不懂定理证明,所以本篇博客不提供\(Matrix\ tree\)定理的证明内容(反正这个东西背结论就可以了是吧) 理解\(Matrix\ ...

  4. [Oracle][Metadata]如何查找与某一个功能相关的数据字典名

    当Oracel的一个新功能出来的时候,我们可能不知道所有与此功能关联的数据字典名称,那么如何才能得到这些 meta data 的 meta data 呢? 可以通过 dicitonary 来查看: 例 ...

  5. [Spark][Python]Spark 访问 mysql , 生成 dataframe 的例子:

    [Spark][Python]Spark 访问 mysql , 生成 dataframe 的例子: mydf001=sqlContext.read.format("jdbc").o ...

  6. BJOI2018简要题解

    BJOI2018简要题解 D1T1 二进制 题意 pupil 发现对于一个十进制数,无论怎么将其的数字重新排列,均不影响其是不是 \(3\) 的倍数.他想研究对于二进制,是否也有类似的性质. 于是他生 ...

  7. 《RabbitMQ Tutorial》译文 第 1 章 简介

    原文来自 RabbitMQ 英文官网的教程(1.Introduction),其示例代码采用了 .NET C# 语言. RabbitMQ is a message broker: it accepts ...

  8. FFMPEG指令

    FFmpeg是一个用于音视频处理的自由软件,被广泛用于音视频开发.FFmpeg功能强大,本文主要介绍如何使用FFmpeg命令行工具进行简单的视频处理. 安装FFmpeg可以在官网下载各平台软件包或者静 ...

  9. Spring RPC 入门学习(3)-获取Student对象

    Spring RPC传递对象. 1. 新建RPC接口:StudentInterface.java package com.cvicse.ump.rpc.interfaceDefine; import ...

  10. Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-D- Array Restoration

    我们知道不满足的肯定是两边大中间小的,这样就用RMQ查询两个相同等值的区间内部最小值即可,注意边界条件 #include<bits/stdc++.h> #define x first #d ...