当前位置:网站首页>(笔记整理未完成)【图论】图的遍历
(笔记整理未完成)【图论】图的遍历
2022-08-02 06:01:00 【gzkeylucky】
知识点
小K 喜欢翻看洛谷博客获取知识。每篇文章可能会有若干个(也有可能没有)参考文献的链接指向别的博客文章。小K 求知欲旺盛,如果他看了某篇文章,那么他一定会去看这篇文章的参考文献(如果他之前已经看过这篇参考文献的话就不用再看它了)。
假设洛谷博客里面一共有 篇文章(编号为 1 到 n)以及
条参考文献引用关系。目前小 K 已经打开了编号为 1 的一篇文章,请帮助小 K 设计一种方法,使小 K 可以不重复、不遗漏的看完所有他能看到的文章。
这边是已经整理好的参考文献关系图,其中,文献 X → Y 表示文章 X 有参考文献 Y。不保证编号为 1 的文章没有被其他文章引用。
请对这个图分别进行 DFS 和 BFS,并输出遍历结果。如果有很多篇文章可以参阅,请先看编号较小的那篇(因此你可能需要先排序)。
输入格式
共 m+1 行,第 1 行为 2 个数,n 和 m,分别表示一共有 篇文章(编号为 1 到 n)以及
条参考文献引用关系。
接下来 m 行,每行有两个整数 X,Y表示文章 X 有参考文献 Y。
输出格式
共 2 行。 第一行为 DFS 遍历结果,第二行为 BFS 遍历结果。
输入输出样例
输入 #1
8 9 1 2 1 3 1 4 2 5 2 6 3 7 4 7 4 8 7 8
输出 #1
1 2 5 6 3 7 8 4 1 2 3 4 5 6 7 8
已AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=1e7+5;
int n,m,a,b;
bool visit[maxn];
int head[maxn],Next[maxn],tot,temp[maxn];
struct Edge
{
int from,to;
}edge[maxn];
inline int read()
{
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=(x<<3)+(x<<1)+(c^48);
c=getchar();
}
return x*f;
}
bool cmp(Edge x,Edge y)
{
if(x.from==y.from) return x.to > y.to;
else return x.from<y.from;
}
inline void add(int u,int v) //建图
{
edge[++tot].to=v;
Next[tot]=head[u];
head[u]=tot;
}
inline void dfs(int x)
{
printf("%d ",x);
visit[x]=true;
for(int j=head[x];j;j=Next[j])
{
if(!visit[edge[j].to])
dfs(edge[j].to);
}
}
void bfs(int x)
{
memset(visit,false,sizeof(visit)); //不要忘记数组清零!!!
queue <int> q;
q.push(1);
visit[1]=true;
while(!q.empty())
{
int z=q.front();
printf("%d ",z);
q.pop();
for(int j=head[z];j;j=Next[j])
{
int y=edge[j].to;
if(visit[y]==false)
{
q.push(edge[j].to);
visit[edge[j].to]=true;
}
}
}
}
int main()
{
n=read();m=read();
for(int i=1;i<=m;++i)
{
edge[i].from=read();
edge[i].to=read();
}
sort(edge+1,edge+m+1,cmp);
for(int i=1;i<=m;++i)
{
add(edge[i].from,edge[i].to);
}
dfs(1);
cout<<endl;
bfs(1);
return 0;
}
边栏推荐
- APT + Transform to realize multi module Application distributed Application life cycle
- MySql -- 不存在则插入,存在则更新或忽略
- HCIP 第三天实验
- PHP Warning: putenv() has been disabled for security reasons in phar
- Dataset: A detailed guide to the download link collection of commonly used datasets in machine learning
- Nodejs安装教程
- 看图就懂|衡量业务增长健康的销售指标如何选择
- odoo field 设置匿名函数domain
- 从入门到精通的MySQL数据库视频教程
- 使用jOOQ 3.14合成外键在视图上写隐式连接
猜你喜欢
振兴农村循环经济 和数链串起农业“生态链”
leetcode solves the linked list merge problem in one step
Launch Space on-premises deployment (local) Beta!
HCIP 第二天
有人开源全凭“为爱发电”,有人却用开源“搞到了钱”
Node installation and configuration of environment variables
MySQL高阶---存储引擎、索引、锁
APP special test: traffic test
MySQL Advanced SQL Statements
Leading the demand and justifying the HR value - the successful launch of the "Human Resource Leading Model HRLM"
随机推荐
HCIP 第三天实验
【21天学习挑战赛】顺序查找
zabbix email alarm and WeChat alarm
Node的安装与环境变量的配置
MarkDown公式指导手册
宝塔+FastAdmin 404 Not Found
How to install the specified version package with NPM and view the version number
MySQL高级SQL语句(二)
node安装及环境变量配置
MySQL高阶---存储引擎、索引、锁
振兴农村循环经济 和数链串起农业“生态链”
Toolbox App 1.25 新功能一览 | 版本更新
Ue after video tutorial first
Launch Space on-premises deployment (local) Beta!
Dataset: A detailed guide to the download link collection of commonly used datasets in machine learning
postgres 多个变量填充字符串,字串格式化
Node installation and environment variable configuration
typescript ‘props‘ is declared but its value is never read 解决办法
HCIP BGP Comprehensive Experiment Establishing peers, route reflectors, federation, route announcement and aggregation
NPM 安装指定版本包的方法及版本号查看