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 std; const int maxn = 150;
const int maxm = 1050; int n, m;//顶点数,边数
int head[maxm], tot;
bool used[maxn];
//head[u]表示已知的最后一条以u为起点的边在边集e中的下标 struct edge {
int to, next;
//e[i].to表示边的终点,e[i].next表示上一条和边e[i]起点相同的点在e中的下标
//int w;权值
}e[maxm];//边集 void init() {
tot = 0;
memset(head, -1, sizeof(head));
memset(used, 0, sizeof(used));
} void add(int u, int v) {//在图中加边
//e[tot].w = w
e[tot].to = v;
e[tot].next = head[u];
head[u] = tot++;
} void dfs(int u) {
used[u] = 1;
printf("%d ", u);
for (int i = head[u]; i != -1; i = e[i].next) {//遍历的方式
int v = e[i].to;
if (!used[v]) dfs(v);
}
} int main() {
while (scanf("%d%d", &n, &m) == 2) {
init();
for (int i = 1; i <= m; ++i) {
int from, to;
scanf("%d%d", &from, &to);
add(from, to);
//add(to, from)无向图
}
for (int i = 1; i <= n; ++i) {
if (!used[i] && head[i] != -1) dfs(i);
}
printf("\n");
}
return 0;
}
/*
5 7
1 2
2 3
3 4
1 3
4 1
1 5
4 5 1 5 3 4 2
*/

链式前向星和邻接表的思想是一样的,

区别就是:邻接表是用链表实现的,可以动态的增加边;

而链式前向星是用结构体数组实现的,是静态的,需要一开始知道数据范围,开好数组大小。

相比之下,邻接表灵活,链式前向星好写。

链式前向星写法下的DFS和BFS的更多相关文章

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

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

  2. 链式前向星DFS

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

  3. 链式前向星+SPFA

    今天听说vector不开o2是数组时间复杂度常数的1.5倍,瞬间吓傻.然后就问好的图表达方式,然后看到了链式前向星.于是就写了一段链式前向星+SPFA的,和普通的vector+SPFA的对拍了下,速度 ...

  4. 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板

    一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...

  5. 图的存储结构:邻接矩阵(邻接表)&链式前向星

    [概念]疏松图&稠密图: 疏松图指,点连接的边不多的图,反之(点连接的边多)则为稠密图. Tips:邻接矩阵与邻接表相比,疏松图多用邻接表,稠密图多用邻接矩阵. 邻接矩阵: 开一个二维数组gr ...

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

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

  7. 链式前向星版DIjistra POJ 2387

    链式前向星 在做图论题的时候,偶然碰到了一个数据量很大的题目,用vector的邻接表直接超时,上网查了一下发现这道题数据很大,vector可定会超的,不会指针链表的我找到了链式前向星这个好东西,接下来 ...

  8. HDU 2544最短路 【dijkstra 链式前向星+优先队列优化】

    最开始学最短路的时候只会用map二维数组存图,那个时候还不知道这就是矩阵存图,也不懂得效率怎么样 经过几个月的历练再回头看最短路的题, 发现图可以用链式前向星来存, 链式前向星的效率是比较高的.对于查 ...

  9. 前向星&链式前向星

    参考博文: https://blog.csdn.net/acdreamers/article/details/16902023 前向星 len[i]以i为起点的边在数组中的存储长度 head[i]以i ...

随机推荐

  1. Hibernate常用方法之_插入

    1.使用session的save方法进行插入 public void saveUser(User user){ Session session = null; Transaction transact ...

  2. C# + ArcEngine 常用方法(不定时更新)

    1.Arcengine调用GP服务,抛出异常方法 object sev = null; try { Application.DoEvents(); gp.Execute(gpBuildPyramids ...

  3. Sublime Text 2.0.2 注册码激活

    直接输入注册码就可以了 ----- BEGIN LICENSE ----- Andrew Weber Single User License EA7E-855605 813A03DD 5E4AD9E6 ...

  4. [Leetcode] Multiply strings 字符串对应数字相乘

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  5. [Leetcode] Same tree判断是否为相同树

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  6. 停课day1

    一早上只做了一个calculator 还是参照题解,好惭愧 f[1]=0; flag[1]=true;    for (int i=2,N=num[n];i<p;i++) {        fo ...

  7. dbcp重连问题排查

    转载自:http://lc87624.iteye.com/blog/1734089 使用数据库连接池时,免不了会遇到断网.数据库挂掉等异常状况,当网络或数据库恢复时,若无法恢复连接池中的连接,那必然会 ...

  8. js+json实现ajax实例

    前期准备: 安装wampserver或者其他相似软件来搭建本地集成安装环境 html.js.css等文件需要放置在wampserver中的www目录中,默认运行index页面 bootstrap.cs ...

  9. ES6学习笔记(四)—— async 函数

    await 是 async wait 的简写, 是 generator 函数的语法糖. async 函数的特点: async 声明一个方法是异步的,await 则等待这个异步方法执行的完成 async ...

  10. 解决Idea Jsp <%%>中 request resopnse等无自动提示的问题

    解决办法:缺少Apache的lib依赖, 只需 File->Project Srructure->Libraries 加号 找到Apache安装的lib目录添加依赖即可.亲测可用