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 ...
随机推荐
- Euler:欧拉函数&素数筛
一.欧拉函数 欧拉函数是小于x的整数中与x互质的数的个数,一般用φ(x)表示. 通式: 其中p1, p2……pn为x的所有质因数,x是不为0的整数. 比如x=12,拆成质因数为12=2*2*3, ...
- coredns CrashLoopBackOff 报错
1.kubectl logs -f coredns-99b9bb8bd-47mvf -n kube-system .:53 2018/09/22 07:39:37 [INFO] CoreDNS-1.2 ...
- FileShare枚举的使用(文件读写锁) - (转载)
开发过程中,我们往往需要大量与文件交互,但往往会出现很多令人措手不及的意外,所以对普通的C#文件操作做了一次总结,问题大部分如下: 写入一些内容到某个文件中,在另一个进程/线程/后续操作中要读取文件内 ...
- Linux java 命令行编译 jar包
Java 命令行编译成class,然后在打包成jar文件. 编译成class javac -classpath $CLASS_PATH -d class ./src/Hello.java 可以通过ja ...
- 计算2个时间之间经过多少Ticks
Ticks是一个周期,存储的是一百纳秒,换算为秒,一千万分之一秒.我们需要计算2个时间之间,经过多少Ticks,可以使用下面的方法来实现,使用2个时间相减. 得到结果为正数,是使用较晚的时间减去较早的 ...
- JVM调优——之CMS 常见参数解析
最近在学习使用CMS这个GC,这里记录下常用的参数. 1. UseCMSCompactAtFullCollection 与 CMSFullGCsBeforeCompaction 有一点需要注意的是:C ...
- C# 队列和栈 线程安全
队列是其元素以先进先出(FIFO)的方式来处理集合,先入队的元素会先读取. 栈是和队列非常类似的另一个容器,栈和队列最大的区别是后进先出(LIFO),也可以说成先进后出. 队列在现实生活中的例子数不胜 ...
- netfilter/iptables 简介
netfilter 是 Linux 内置的一种防火墙机制,我们一般也称之为数据包过滤机制.iptables 则是一个命令行工具,用来配置 netfilter 防火墙.下图展示了一个带有防火墙的简单网络 ...
- C#爬虫基本知识
url编码解码 首先引用程序集System.Web.dll 如果要解码某个url的参数值的话,可以调用下面的方法: System.Web.HttpUtility.UrlDecode(string) 对 ...
- Finished yeah!
终于到了最后的博客阶段,这时候才知道博客此时此刻是多么的惬意,它成了书写心声的自由平台!耗时一天完成这作业说起来也是蛮辛苦的,编译器需要新装,IDE需要熟悉,当然最主要的是之前浅入浅出的C++功底在此 ...