SPOJ #442 Searching the Graph
Just CS rookie practice on DFS\BFS. But details should be taken care of:
1. Ruby implementation got TLE so I switched to C++
2. There's one space after each output node, including the last one. But SPOJ doesn't clarify it clearly.
// #include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
using namespace std; typedef map<int, vector<int> > Graph; void bfs(Graph &g, int key)
{
vector<int> visited; visited.reserve(g.size());
for(unsigned ic = ; ic < g.size(); ic ++)
{
visited[ic] = ;
} queue<int> fifo;
fifo.push(key);
while(!fifo.empty())
{ int cKey = fifo.front();
fifo.pop(); if(visited[cKey - ] == )
{
cout << cKey <<" "; vector<int> &ajVec = g[cKey];
for(unsigned i = ; i < ajVec.size(); i ++)
{
fifo.push(ajVec[i]);
}
visited[cKey - ] = ;
}
} cout << endl;
} vector<int> dfs_rec;
void initDfsRec(Graph &g)
{
dfs_rec.clear();
unsigned gSize = g.size();
dfs_rec.reserve(gSize);
for(unsigned ic = ; ic < gSize; ic ++)
{
dfs_rec[ic] = ;
}
} void dfs(Graph &g, int key)
{
cout << key << " ";
dfs_rec[key - ] = ;
vector<int> &ajVec = g[key];
for(unsigned i = ; i < ajVec.size(); i ++)
{
if(dfs_rec[ajVec[i] - ] == )
{
dfs(g, ajVec[i]);
}
}
} int main()
{ int runcnt = ;
cin >> runcnt;
for(int i = ; i < runcnt; i ++)
{
Graph g; int nvert = ; cin >> nvert;
for(int n = ; n < nvert; n ++)
{
int vid = ; cin >> vid;
int cnt = ; cin >> cnt;
vector<int> ajVec;
for(int k = ; k < cnt; k ++)
{
int aj = ; cin >> aj;
ajVec.push_back(aj);
} g.insert(Graph::value_type(vid, ajVec));
} //
cout << "graph " << i + << endl; int ikey = , iMode = ;
cin >> ikey >> iMode;
while(!(ikey == && iMode == ))
{
if(iMode == )
{
initDfsRec(g);
dfs(g, ikey);
cout <<endl;
}
else if(iMode == )
{
bfs(g, ikey);
}
cin >> ikey >>iMode;
}
} return ;
}
SPOJ #442 Searching the Graph的更多相关文章
- Codeforces Round #236 (Div. 2) C. Searching for Graph(水构造)
题目大意 我们说一个无向图是 p-interesting 当且仅当这个无向图满足如下条件: 1. 该图恰有 2 * n + p 条边 2. 该图没有自环和重边 3. 该图的任意一个包含 k 个节点的子 ...
- 构造图 Codeforces Round #236 (Div. 2) C. Searching for Graph
题目地址 /* 题意:要你构造一个有2n+p条边的图,使得,每一个含k个结点子图中,最多有2*k+p条边 水得可以啊,每个点向另外的点连通,只要不和自己连,不重边就可以,正好2*n+p就结束:) */ ...
- C. Searching for Graph(cf)
C. Searching for Graph time limit per test 1 second memory limit per test 256 megabytes input standa ...
- CF_402C Searching for Graph 乱搞题
题目链接:http://codeforces.com/problemset/problem/402/C /**算法分析: 乱搞题,不明白题目想考什么 */ #include<bits/stdc+ ...
- Codeforces Round #236 (Div. 2)
A. Nuts time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inputoutput:st ...
- Reading task(Introduction to Algorithms. 2nd)
Introduction to Algorithms 2nd ed. Cambridge, MA: MIT Press, 2001. ISBN: 9780262032933. Introduction ...
- apache atlas源码编译打包 centos
参考:https://atlas.apache.org/InstallationSteps.html https://blog.csdn.net/lingbo229/article/details/8 ...
- Clone Graph leetcode java(DFS and BFS 基础)
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
- SPOJ 375. Query on a tree (树链剖分)
Query on a tree Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Ori ...
随机推荐
- Tomcat中负载的Session解决办法
Tomcat进行请求的Session解决方式总体来说有三种, (1)使用Nginx或者Apache反向代理工具进行用户请求的分机器,在Tomcat的Engine中的标签中加入jvmRoute属性,指定 ...
- 初识mySQL(关系型数据库)
一.数据库修改密码 ①先执行use mysql; ②再执行update mysql.user set password=PASSWORD(要修改的密码) where user='root' ; ...
- cocos2d-html5版日历组件
根据一as3版本给改编成成了js版的.as3版本的地址: http://download.csdn.net/detail/fengye513/2722289 胜利项目做的差不多了,由于项目用的是c+ ...
- Xen虚拟机磁盘镜像模板制作(四)—CentOS 7
在<Xen虚拟机磁盘镜像模板制作(三)—CentOS 7>一文中,我们已经成功制作出了 CentOS7 磁盘镜像.下面我们说明下如何通过它来生成目标虚拟机,同时测试下之前制作好的虚拟机磁盘 ...
- C# string[,]与string[][]的区别
对于这两者的区别: 1.入门:string[,]可读可写,而string[][]与string[]相同,不可对第二位进行写操作 static void Main(string[] args) { // ...
- 十一 SOA 与 ESB
一 SOA 1. 定义: a) 一种充分利用开放标准,将软件资产展现为服务的结构. b) 提供软件资产标准的展现和交互途径 c) 在开发其他应用的时候,可以将独立的软件资产封装为一个一个的“积木”. ...
- iOS学习笔记---oc语言第二天
实例变量与方法 一.实例变量的可见度 二.方法 oc中的方法分两种:类方法和实例方法 类方法:只能类使用 eg:+ (id)alloc 注:类方法中不能使用实例变量 实例方法:只能对象使用,eg:- ...
- 移动开发中使用Onsen UI的笔记
onsen var m_index = ons.bootstrap() 初始化框架 m_index 赋值 增加对象. m_index.value('getUser',{ // user:window. ...
- 关于string的练习题目
/*Are they equal*/#include<iostream>#include<string>using namespace std;int n;string dea ...
- Validform:一行代码搞定整站的表单验证!
表单验证不再发愁,http://validform.rjboy.cn/