图的深度遍历
来源:互联网 发布:java drawimage 编辑:程序博客网 时间:2024/05/16 12:47
图的深度遍历
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
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
#include<iostream>#include<cstring>using namespace std;bool p[101][101],vis[101];int m,n;void pop(int x){ int i; for(i=0;i<n;i++) { if(!vis[i]&&p[x][i]) { cout<<" "<<i; vis[i]=true; pop(i); } }}int main(){ int t,a,b,i; cin>>t; while(t--) { cin>>n>>m; memset(p,false,sizeof(p)); memset(vis,false,sizeof(vis)); for(i=0;i<m;i++) { cin>>a>>b; p[a][b]=true; p[b][a]=true; } vis[0]=true; cout<<"0"; pop(0); cout<<endl; } return 0;}/***************************************************User name: Today仔仔Result: AcceptedTake time: 0msTake Memory: 160KBSubmit time: 2016-11-11 18:16:30****************************************************/
0 0
- 图的深度遍历
- 图的深度遍历
- 图的深度遍历
- 图的深度遍历
- 图的深度遍历
- 图的深度遍历
- 图的深度遍历
- 图的深度遍历
- 图的深度遍历
- 图的深度遍历
- 图的深度遍历
- 图的深度遍历
- 图的深度遍历
- 图的深度遍历
- 图的深度遍历
- 图的深度遍历
- 图的深度遍历
- 图的深度遍历
- 作业七-ID-1050-Problem B: Sequence Problem : Array Practice
- 水了,,
- 448. Find All Numbers Disappeared in an Array
- linux中的硬链接和软链接
- Maven笔记(4)在Eclipse创建Maven工程
- 图的深度遍历
- Uva122 Trees on the level【指针建立树+bfs遍历树】【例题6-7】
- linux把文件放置到剪贴板
- Linux网络配置
- 趣味代码:水果动物派对
- web相关知识总结
- 40个Java多线程问题总结
- sharedpreferences保存用户设置
- linux进程改名