数据结构实验之图论二:图的深度遍历
来源:互联网 发布:江西省公务员待遇知乎 编辑:程序博客网 时间:2024/04/29 05:35
数据结构实验之图论二:图的深度遍历
Problem Description
请定一个无向图,顶点编号从0到n-1,用深度优先搜索(DFS),遍历并输出。遍历时,先遍历节点编号小的。
Input
输入第一行为整数n(0 < n < 100),表示数据的组数。 对于每组数据,第一行是两个整数k,m(0 < k < 100,0 < m < k*k),表示有m条边,k个顶点。 下面的m行,每行是空格隔开的两个整数u,v,表示一条连接u,v顶点的无向边。
Output
输出有n行,对应n组输出,每行为用空格隔开的k个整数,对应一组数据,表示DFS的遍历结果。
Example Input
14 40 10 20 32 3
Example Output
0 1 2 3
Hint
Author
#include<bits/stdc++.h>
using namespace std;
int s[110][110], vist[110];
int n, m, num;
void create(int m)
{
int u, v;
while(m--)
{
cin>>u>>v;
s[u][v] = s[v][u] = 1;
}
}
void DFS(int x)
{
vist[x] = 1;
num++;
if(num == n)
cout<<x<<endl;
else
cout<<x<<' ';
for(int i = 0; i < n; i++)
{
if(!vist[i]&&s[x][i] == 1)
DFS(i);
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
num = 0;
memset(s,0,sizeof(s));
memset(vist,0,sizeof(vist));
create(m);
DFS(0);
}
return 0;
}
- 数据结构实验之图论二:图的深度遍历
- 数据结构实验之图论二:图的深度遍历
- 数据结构实验之图论二:图的深度遍历
- 数据结构实验之图论二:图的深度遍历
- 数据结构实验之图论二:图的深度遍历
- 数据结构实验之图论二:图的深度遍历
- 数据结构实验之图论二:图的深度遍历
- 数据结构实验之图论二:图的深度遍历
- 数据结构实验之图论二:图的深度遍历
- 数据结构实验之图论二:图的深度遍历
- 数据结构实验之图论二:图的深度遍历
- 数据结构实验之图论二:图的深度遍历
- 数据结构实验之图论二:图的深度遍历
- 2107 数据结构实验之图论二:图的深度遍历
- 数据结构实验之图论二:图的深度遍历
- 数据结构实验之图论二:图的深度遍历
- 数据结构实验之图论二:图的深度遍历
- 数据结构实验之图论二:图的深度遍历
- Fastlane构建ipa包流程小结 -- match 篇
- Android 开发之 View.post() 的作用
- 运行script_faster_rcnn_VOC2007_ZF.m出现的错误
- AI 学习之路——轻松初探 Python 篇(一)
- FastJson一些常见方法(API)
- 数据结构实验之图论二:图的深度遍历
- 面向对象--异常
- Spring IOC 源码分析-bean标签解析
- Cannot find System Java Compiler. Ensure that you have installed a JDK (not just a JRE)
- android listview多布局
- Oracle优化面试题
- java串口通信
- Unity3D
- 带你逐步深入了解SSM框架——淘淘商城项目之框架整合,后台系统搭建