首页 > 其他分享 >图的表示与遍历

图的表示与遍历

时间:2024-01-26 19:55:23浏览次数:26  
标签:表示 遍历 const int namespace vis printf

讲解


 

 

 

 

 

 

 

 

 

 

 代码  P5318【深基18.例3】查找文献


 

#include <bits/stdc++.h>
using namespace std;

const int N=100010;
vector<int> a[N];
int n, m, x, y, vis[N];
queue<int> q;
void dfs(int s)
{
	if (vis[s]==1) return;
	vis[s]=1;
	printf("%d ", s);
	for (int i=0; i<a[s].size(); i++)
		dfs(a[s][i]);
}
void bfs(int s)
{
	printf("%d ", s);
	q.push(s);
	vis[s]=1;
	while (!q.empty())
	{
		int t=q.front();
		q.pop();
		for (int i=0; i<a[t].size(); i++)
			if (vis[a[t][i]]!=1) 
			{
				vis[a[t][i]]=1;
				q.push(a[t][i]);
				printf("%d ", a[t][i]);
			}
	}
}
int main()
{
	scanf("%d%d", &n, &m);	
	//建图 
	for (int i=1; i<=m; i++)
	{
		scanf("%d%d", &x, &y);
		a[x].push_back(y);
		//a[y].push_back(x);   有向图无需存 
	}
	for (int i=1; i<=n; i++)
		sort(a[i].begin(), a[i].end());
	
	//遍历
	memset(vis, 0, sizeof(vis));
	dfs(1);
	printf("\n");
	memset(vis, 0, sizeof(vis));
	bfs(1);
	return 0;
}

 

标签:表示,遍历,const,int,namespace,vis,printf
From: https://www.cnblogs.com/didiao233/p/17990575

相关文章

  • golang 遍历目录的两种方式、删除目录的两种方式
    funcmain(){ directory:="/Users/mike/Downloads" //不会递归只会读取当前的单层目录 directories,err:=os.ReadDir(directory) iferr!=nil{ fmt.Println(err) } for_,d:=rangedirectories{ fmt.Println(d.Name(),d.IsDir()) } //会递归遍历所......
  • 树的遍历
    二叉树的遍历前序遍历#include<bits/stdc++.h>usingnamespacestd;intn;structs{ intl,r,d;}a[10005];voidf(intt)//前序{ if(t==0)return; cout<<t<<''; f(a[t].l); f(a[t].r);}intmain(){cin>>n;for(inti=1;i<......
  • KY11 二叉树遍历C++
    这个题目思路其实就是先序遍历的变形。相当于沿着先序遍历的顺序跟着构建二叉树就行。然后中序遍历这个树。#include<iostream>#include<string>usingnamespacestd;structtnode{chardata;structtnode*left;structtnode*right;};typedefstructt......
  • KY212 二叉树遍历C++
    思路是先构造出树,然后在后序遍历整个树。#include<iostream>#include<string>usingnamespacestd;structTnode{chardata;structTnode*left;structTnode*right;};typedefstructTnodeTree;Tree*build(stringpre,inth1,intt1,stringin,inth2......
  • 2024-1-24案例(地区查询)以及遍历方法
    目录案例(地区查询)步骤解析案例里面的map方法该案例的最后一个将数据插入到页面上案例(地区查询)需求:根据输入的省份名字和城市名字,查询地区并渲染列表步骤首先:确定URL网址和参数说明查询某个省内某个城市的所有地区参数名:pname:省份名字或直辖市名字,比如北京、福建省、辽......
  • 树结构及前中后续遍历
    publicclassTree{publicstaticvoidmain(String[]args){Treeroot=newTree(50);Tree.insert(root,30);Tree.insert(root,60);Tree.insert(root,70);Tree.insert(root,100);Tree.insert(root,80);......
  • 遍历删除集合元素
    1publicclassTest{2publicstaticvoidmain(String[]args){3List<String>list=newArrayList<>();4list.add("张三");5list.add("张三");6list.add("李四");7......
  • 计算机编程中的黑魔法编程是什么?如何求解一个浮点数的平方根倒数?计算机中的浮点数是如
    原视频:没有显卡的年代,这群程序员用4行代码优化游戏最原始的求解目标:(求一个浮点数的开方的导数)浮点数在计算机中的表示形式:对数的运算法则:A为a在计算机中的表示形式(二进制表示形式):求浮点数的平方根倒数的应用场景:这个情况,直白的说就......
  • 二进制数表示数据
    计算机信息用二进制数表示:IC有几种不同的形状,有的像一条黑色蜈蚣,在其两侧有数个乃至数百个引脚;有的则像插花用的针盘,引脚在IC内部并排排列着。IC的所有引脚,只有直流电压0V或5V²两个状态。也就是说,IC的一个引脚,只能表示两个状态。IC的这个特性,决定了计算机的信息数据只能用二进......
  • 图的遍历
    链式前向星存图点击查看代码#include<bits/stdc++.h>usingnamespacestd;inth[100005],nx[100005],t[100005],cnt;intans[100005];intread1(){ charcc=getchar(); while(!(cc>=48&&cc<=57)) { if(cc=='-') { break; } cc=getch......