dfs和bfs(链式前向星实现)
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(链式前向星实现)的更多相关文章
- 链式前向星存树图和遍历它的两种方法【dfs、bfs】
目录 一.链式前向星存图 二.两种遍历方法 一.链式前向星存图:(n个点,n-1条边) 链式前向星把上面的树图存下来,输入: 9 ///代表要存进去n个点 1 2 ///下面是n-1条边,每条边连接两 ...
- 链式前向星写法下的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 ...
- poj-1459-最大流dinic+链式前向星-isap+bfs+stack
title: poj-1459-最大流dinic+链式前向星-isap+bfs+stack date: 2018-11-22 20:57:54 tags: acm 刷题 categories: ACM ...
- Pants On Fire(链式前向星存图、dfs)
Pants On Fire 传送门:链接 来源:upc9653 题目描述 Donald and Mike are the leaders of the free world and haven't ...
- zzuli 2130: hipercijevi 链式前向星+BFS+输入输出外挂
2130: hipercijevi Time Limit: 1 Sec Memory Limit: 128 MB Submit: 595 Solved: 112 SubmitStatusWeb B ...
- 链式前向星BFS
本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5399068 采用链式前向星的BFS: #include <iostream> #incl ...
- 链式前向星DFS
本文链接:http://www.cnblogs.com/Ash-ly/p/5399057.html 采用链式前向星存图的DFS: #include <iostream> #include ...
- zzuli 2131 Can Win dinic+链式前向星(难点:抽象出网络模型+建边)
2131: Can Win Time Limit: 1 Sec Memory Limit: 128 MB Submit: 431 Solved: 50 SubmitStatusWeb Board ...
- 网络流dinic模板,邻接矩阵+链式前向星
//这个是邻接矩阵的#include<iostream> #include<queue> #include<string.h> #include<stdio. ...
- 最短路 spfa 算法 && 链式前向星存图
推荐博客 https://i.cnblogs.com/EditPosts.aspx?opt=1 http://blog.csdn.net/mcdonnell_douglas/article/deta ...
随机推荐
- PAT A1033 To Fill or Not to Fill (25 分)——贪心
With highways available, driving a car from Hangzhou to any other city is easy. But since the tank c ...
- 20175105 2018-2019-2 《Java程序设计》第八周学习总结
20175105 2018-2019-2 <Java程序设计>第八周学习总结 教材学习内容总结 第十五章主要内容有:泛型.链表.堆栈.散列映射.树集以及树映射. 泛型:可以使用class名 ...
- SkylineGlobe Android 开发 面积计算示例代码
SkylineGlobe Android 开发 面积计算示例代码: 如果之前熟悉SkylineGlobe桌面端的二次开发,看这些代码应该不难理解. package com.skyline.terrae ...
- 8-51单片机ESP8266学习-AT指令(单片机采集温湿度数据通过8266发送给C#TCP客户端显示)
http://www.cnblogs.com/yangfengwu/p/8785516.html 先写单片机端的程序 先把源码和资料链接放到这里 链接: https://pan.baidu.com/s ...
- java读取写入文件
先来看一下java写入文件 public static void readTxt(String value) throws IOException { FileWriter fw = null; tr ...
- 3.3《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)——less即more
Unix提供了两个工具查看不止文件的头部和尾部.这个功能程序叫做more,但有种更强大的变异体叫做less(起初我认为这是玩笑).less这个程序是交互性地,所以很难在输出时捕获,但是仍然为大家提供了 ...
- CSS-Photoshop投影与CSS中box-shadow的转换
box-shadow是给元素块添加周边阴影效果基本语法是: {box-shadow:[inset] x-offset y-offset blur-radius spread-radiuscolor} ...
- 搭建Zookeepeer源码工程
一.搭建ant环境 1.下载ant&将ant解压至安装目录 http://ant.apache.org/bindownload.cgi 2.配置环境变量 ANT_HOME:配置ant的安装目录 ...
- 换了电脑如何使用hexo继续写博客
前言 我们知道,使用 Github+hexo 搭建一个个人博客确实需要花不少时间的,我们搭好博客后使用的挺好,但是如果我们有一天电脑突然坏了,或者换了系统,那么我们怎么使用 hexo 再发布文章到个人 ...
- Http指南(1)
网关:是一种特殊的服务器,作为其他服务器的中间实体使用; Agent代理:所有发布web请求的应用程序都是HTTP Agent代理.Web浏览器其实就是一种代理; HTTP报文是在HTTP应用程序之间 ...