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 ...
随机推荐
- SourceTree不出现用户登录窗口,提示错误fatal: unable to access'...'; error setting certificate verify locations
SourceTree不出现用户登录窗口,提示错误fatal: unable to access'...'; error setting certificate verify locations; .. ...
- hdu 3666 Making the Grade
题目大意 给出了一列数,要求通过修改某些值,使得最终这列数变成有序的序列,非增或者非减的,求最小的修改量. 分析 首先我们会发现,最终修改后,或者和前一个数字一样,或者和后一个数字一样,这样才能修改量 ...
- [转载] 动态链接库dll的 静态加载 与 动态加载
转载自:http://blog.csdn.net/youxin2012/article/details/11538491 dll 两种链接方式 : 动态链接和静态链接(链接亦称加载) 动态链接是 ...
- 经典 socket通讯 -- 已验证
[tcp通信] 来源:不详: client: package com.defonds.socket.begin; import java.io.BufferedReader; import java. ...
- C++ Primer : 第十二章 : 文本查询程序
C++ Primer书上这个例子讲的很不错,写写帮助自己理解标准库和智能指针. .h 文件内容 #include <fstream> #include <iostream> # ...
- iOS应用日志:开始编写日志组件与异常日志
应用日志(一):开始编写日志组件 对于那些做后端开发的工程师来说,看 LOG解Bug应该是理所当然的事,但我接触到的移动应用开发的工程师里面,很多人并没有这个意识,查Bug时总是一遍一遍的试图重现,试 ...
- JS中的自定义属性
<div id="div1" a="a" data-bbb="bbb">div</div> <script&g ...
- Linux 的账号与群组[转自vbird]
Linux 的账号与群组 管理员的工作中,相当重要的一环就是『管理账号』啦!因为整个系统都是你在管理的, 并且所有一般用户的账号申请,都必须要透过你的协助才行!所以你就必须要了解一下如何管理好一个服务 ...
- dll--二进制层面的复用
积木式思想其实是很自然的一个过程,从c的库函数到C++的标准库,再到dll.com.com+都是这种思想推动下的结果,和现实生活中的人们的思维方式并无二致,只不过软件是在一个虚拟的世界中,并分化出许多 ...
- Redis server&Client executions
https://www.nuget.org/packages/Microsoft.Extensions.Caching.Redis/ https://github.com/StackExchange/ ...