洛谷P5318 【深基18.例3】查找文献

https://www.luogu.com.cn/problem/P5318

复制代码
#include<bits/stdc++.h>
using namespace std;
int n,m;
vector<int> e[100005];
bool vis[100005];
void dfs(int u){
	cout<<u<<" ";
	for(int i=0;i<e[u].size();i++){
		int v=e[u][i];
		if(!vis[v]){
			vis[v]=1;
			dfs(v);
		}
		
	}
} 
void bfs(){
	memset(vis,0,sizeof(vis));
	queue<int>q;
	q.push(1);
	vis[1]=1;
	while(!q.empty()){
		int u=q.front();
		q.pop();
		cout<<u<<" ";
		for(int i=0;i<e[u].size();i++){
			int v=e[u][i];
			if(!vis[v]){
				vis[v]=1;
				q.push(v);
			}
		}
	}
} 
int main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int u,v;
		cin>>u>>v;
		e[u].push_back(v);
	}
	for(int i=1;i<=n;i++){
		sort(e[i].begin(),e[i].end());
	} 
	vis[1]=1;
	dfs(1);
	cout<<endl;
	bfs();
	return 0;
}
相关推荐
Greedy Alg21 小时前
LeetCode 208. 实现 Trie (前缀树)
算法
Kt&Rs21 小时前
11.5 LeetCode 题目汇总与解题思路
数据结构·算法·leetcode
还是码字踏实21 小时前
基础数据结构之数组的前缀和技巧:和为K的子数组(LeetCode 560 中等题)
算法·leetcode·前缀和·哈希字典
沙威玛_LHE1 天前
树和二叉树
数据结构·算法
py有趣1 天前
LeetCode算法学习之两数之和 II - 输入有序数组
学习·算法·leetcode
夏鹏今天学习了吗1 天前
【LeetCode热题100(62/100)】搜索二维矩阵
算法·leetcode·矩阵
吃着火锅x唱着歌1 天前
LeetCode 1128.等价多米诺骨牌对的数量
算法·leetcode·职场和发展
十八岁讨厌编程1 天前
【算法训练营 · 补充】LeetCode Hot100(中)
算法·leetcode
橘颂TA1 天前
【剑斩OFFER】算法的暴力美学——最小覆盖字串
算法·c/c++·就业
wearegogog1231 天前
基于混合蛙跳算法和漏桶算法的无线传感器网络拥塞控制与分簇新方法
网络·算法