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 ...
随机推荐
- Linux x64 -- 内核程序(驱动程序)读取任意进程数据实现
四级页表结构 现在的64位Linux系统中,并没有使用全部的64位地址空间,而是使用其低48位,高16位并没有使用. 其中 39至47这9位用于索引PGD(page global directory) ...
- SpringMVC异常处理方式
一.描述 在J2EE项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的.不可预知的异常需要处理.每个过程都单独处理异常,系统的代码耦合 ...
- 解决Skyline 6.5版本中3DML模型单体化后外部网页挂接问题
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Oracle 将一个查询结果值动态赋值给一个变量
在写存储过程或函数时,经常需要用到中间变量,需要将一些值做临时存储. 可以通过动态变量方式来赋值.如下: FUNCTION YOUR_FUN (VAL1 IN NVARCHAR2) RETURN NC ...
- 【转】Java生成图片验证码
原文转自:http://blog.csdn.net/ruixue0117/article/details/22829557 看了挺多图片验证码的代码,感觉没什么长的好看点的,就自己动手写了个,写完发现 ...
- vba基础知识
VBA中dim,static和public,private的区别 转载的文章:出自 红袖的博客http://blog.sina.com.cn/19850701xx 1.Public和Priva ...
- C# 程序默认使用管理员权限(转载)
1.从A程序启动B程序,当B程序需要管理员权限的时候代码如下 A程序里面启动B程序的代码如下 ProcessStartInfo startInfo = new ProcessStartInfo(); ...
- Python基础语法复习
1.数据类型 List 列表 函数 append(): 在列表末尾追加. count(): 计算对象在列表中出现的次数. extend():将列表内容添加到列表中. index(): 计算对象在列表中 ...
- Winio驱动在64位windows下无法使用的解决方法
C#在使用WinIo的驱动开发类似按键精灵一类工具的时候,需要对相关的驱动进行注册才能正常启动,找了下资料,资料来自: http://jingyan.baidu.com/article/642c9d3 ...
- [Oracle]发生 ora-06502 RMAN 在对 catalog DB 同期时出错的调查方法
Catalog DB resync error: 1, setting on the server that starts the RMAN client $ Export EVENT_10928 = ...