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 ...
随机推荐
- 兼容性所有浏览器的透明CSS设置
兼容所有浏览器的透明CSS设置: .transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; -khtml-opacity: 0. ...
- 合并两个有序数组a和b到c
问题:两个有序数组a和b,合并成一个有序数组c. // 合并两个有序数组a和b到c void Merge_Array(int a[], int n, int b[], int m, int c[]) ...
- MapReduce 计算模型
前言 本文讲解Hadoop中的编程及计算模型MapReduce,并将给出在MapReduce模型下编程的基本套路. 模型架构 在Hadoop中,用于执行计算任务(MapReduce任务)的机器有两个角 ...
- 冒泡排序(C++版)
/** Bubble Sort * * Key * * position: where swap * * iter: sub-position in each trip * */ template & ...
- 83. Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- ZOJ 1088 System Overload
原题链接 题目大意:浙大的破网络容量有限,太多人同时使用BBS就会系统崩溃.为了保持系统正常工作,过载时就需要切断部分用户.规则是把玉泉所有的建筑从1到n编号,设定一个常数m.从1开始数,第m幢建筑的 ...
- ZOJ 1002 Fire Net
题目大意:有一个4*4的城市,其中一些格子有墙(X表示墙),在剩余的区域放置碉堡.子弹不能穿透墙壁.问最多可以放置几个碉堡,保证它们不会相互误伤. 解法:从左上的顶点开始遍历,如果这个点不是墙,做深度 ...
- Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 整理的一些免费的Android项目实战系列视频教程
http://blog.itpub.net/29737144/viewspace-1212539/
- Codeforces Round #373 (Div. 1)
Codeforces Round #373 (Div. 1) A. Efim and Strange Grade 题意 给一个长为\(n(n \le 2 \times 10^5)\)的小数,每次可以选 ...