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的更多相关文章

  1. Codeforces Round #236 (Div. 2) C. Searching for Graph(水构造)

    题目大意 我们说一个无向图是 p-interesting 当且仅当这个无向图满足如下条件: 1. 该图恰有 2 * n + p 条边 2. 该图没有自环和重边 3. 该图的任意一个包含 k 个节点的子 ...

  2. 构造图 Codeforces Round #236 (Div. 2) C. Searching for Graph

    题目地址 /* 题意:要你构造一个有2n+p条边的图,使得,每一个含k个结点子图中,最多有2*k+p条边 水得可以啊,每个点向另外的点连通,只要不和自己连,不重边就可以,正好2*n+p就结束:) */ ...

  3. C. Searching for Graph(cf)

    C. Searching for Graph time limit per test 1 second memory limit per test 256 megabytes input standa ...

  4. CF_402C Searching for Graph 乱搞题

    题目链接:http://codeforces.com/problemset/problem/402/C /**算法分析: 乱搞题,不明白题目想考什么 */ #include<bits/stdc+ ...

  5. Codeforces Round #236 (Div. 2)

    A. Nuts time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inputoutput:st ...

  6. Reading task(Introduction to Algorithms. 2nd)

    Introduction to Algorithms 2nd ed. Cambridge, MA: MIT Press, 2001. ISBN: 9780262032933. Introduction ...

  7. apache atlas源码编译打包 centos

    参考:https://atlas.apache.org/InstallationSteps.html https://blog.csdn.net/lingbo229/article/details/8 ...

  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. ...

  9. SPOJ 375. Query on a tree (树链剖分)

    Query on a tree Time Limit: 5000ms Memory Limit: 262144KB   This problem will be judged on SPOJ. Ori ...

随机推荐

  1. dede的pagelist标签的listsize数字属性详解(借鉴)

    dede的pagelist标签的listsize数字属性详解.见远seo经常用织梦搭建各种网站,有次发现列表页面的分页显示超过div的界限,也就是溢出了或者说是撑破了.后来经过研究发现是pagelis ...

  2. css 包含的图片和style="display:none"可以避免图片加载,可以节省网络流量

    从别人那儿学到一招:先记录下来: <head> <meta charset="UTF-8"> <title>Document</title ...

  3. vs2012相关配置

    文件包含不能在当前代码页(936)中表示的字符.请将该文件保存为 Unicode 格式: 在Project -> Properties -> Configuration Propertie ...

  4. C语言redirection

    1)把执行文件xxx.exe 和读取的文件yyy.txt放在一个地方 2)用dos 命令 dir 和cd,打开存放文件的文件夹 3)window 7 情况下 输入xxx.exe < yyy.tx ...

  5. 高效而轻松的sed命令

    sed(stream editor)是一款高效的流编辑器,它一次只处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的 ...

  6. 行为识别笔记:improved dense trajectories算法(iDT算法)(转载)

    iDT算法是行为识别领域中非常经典的一种算法,在深度学习应用于该领域前也是效果最好的算法.由INRIA的IEAR实验室于2013年发表于ICCV.目前基于深度学习的行为识别算法效果已经超过了iDT算法 ...

  7. Codeforces Round #308 (Div. 2) A B C 水 数学

    A. Vanya and Table time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. 批量kill相关所有进程

    首先,用ps查看进程,方法如下: $ ps -ef …… smx       1822     1  0 11:38 ?        00:00:49 gnome-terminal smx      ...

  9. UVa 1394 约瑟夫问题的变形

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  10. Tomcat的JVM优化

    一.JVM管理内存段分类 1.线程共享内存 方法区:存储jvm加载的class.常量.静态变量.及时编译器编译后的代码等 java堆:存储java所有对象实例.数组等 2.线程私有内存 程序计数寄存器 ...