2014-03-19 03:32

题目:给定一个有向图,判断其中两点是否联通。

解法:DFS搜索解决,如果是无向图的话,就可以用并查集高效解决问题了。

代码:

 // 4.2 Write a program to check if there exists a path between two nodes in a directed graph.
#include <algorithm>
#include <cstdio>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std; struct GraphNode {
int label;
unordered_set<GraphNode *> neighbors; GraphNode(int _label = ): label(_label) {};
}; bool hasPath(GraphNode *n1, GraphNode *n2, unordered_set<GraphNode *> &checked, vector<GraphNode *> &path)
{
if (n1 == nullptr || n2 == nullptr) {
return false;
} checked.insert(n1);
path.push_back(n1); if (n1 == n2) {
return true;
} unordered_set<GraphNode *>::iterator it;
for (it = n1->neighbors.begin(); it != n1->neighbors.end(); ++it) {
if (checked.find(*it) == checked.end()) {
if (hasPath(*it, n2, checked, path)) {
return true;
}
}
}
checked.erase(n1);
path.pop_back();
return false;
} void constructGraph(int n, vector<GraphNode *> &nodes)
{
int i;
int ne, x, y;
int label; nodes.resize(n);
for (i = ; i < n; ++i) {
scanf("%d", &label);
nodes[i] = new GraphNode(label);
} scanf("%d", &ne);
for (i = ; i < ne; ++i) {
scanf("%d%d", &x, &y);
nodes[x]->neighbors.insert(nodes[y]);
}
} void clearGraph(vector<GraphNode *> &nodes)
{
int n = (int)nodes.size();
int i; for (i = ; i < n; ++i) {
nodes[i]->neighbors.clear();
delete nodes[i];
nodes[i] = nullptr;
}
nodes.clear();
} int main()
{
int n;
vector<GraphNode *> nodes;
vector<GraphNode *> path;
unordered_set<GraphNode *> checked;
int idx1, idx2; while (scanf("%d", &n) == && n > ) {
constructGraph(n, nodes);
while (scanf("%d%d", &idx1, &idx2) == && (idx1 >= && idx2 >= )) {
if (idx1 >= n || idx2 >= n) {
continue;
} if (hasPath(nodes[idx1], nodes[idx2], checked, path)) {
printf("Yes\n");
printf("%d", path[]->label);
for (int i = ; i < (int)path.size(); ++i) {
printf("->%d", path[i]->label);
}
printf("\n");
} else {
printf("No\n");
}
checked.clear();
path.clear();
}
clearGraph(nodes);
} return ;
}

《Cracking the Coding Interview》——第4章:树和图——题目2的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  5. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  6. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  7. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  8. Cracking the Coding Interview 150题(二)

    3.栈与队列 3.1 描述如何只用一个数组来实现三个栈. 3.2 请设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值.pop.push和min三个方法的时间复杂度必须为O( ...

  9. 《Cracking the Coding Interview》——第4章:树和图——题目9

    2014-03-19 05:07 题目:给定一棵二叉树T和一个值value,在T中找出所有加起来和等于value的路径.路径的起点和终点都可以是树的任意节点. 解法:我偷了个懒,直接把这棵树看成一个无 ...

  10. 《Cracking the Coding Interview》——第4章:树和图——题目8

    2014-03-19 05:04 题目:给定两棵二叉树T1和T2,判断T2是否是T1的子树.子树的定义是,以T1的某个节点(可以是T1的根)作为根节点,得到的这棵树和T2一模一样. 解法:首先可以根据 ...

随机推荐

  1. 如何在 ubuntu linux 一行中执行多条指令

    cd /my_folder rm *.jar svn co path to repo mvn compile package install 使用&& 运算符连接指令 cd /my_f ...

  2. 关于(void**)及其相关的理解

    #define LOADBASSFUNCTION (f) *((void **)&f)=(void*)GetProcAddress (hBass,# f) 这一句话使用*((void**)&a ...

  3. vi使用命令

    二.移动光标类命令 h :光标左移一个字符 l :光标右移一个字符 space:光标右移一个字符 Backspace:光标左移一个字符 k或Ctrl+p:光标上移一行 j或Ctrl+n :光标下移一行 ...

  4. SQL-常用数据类型

    整数除了 INT 外,还有 TINYINT.SMALLINT.MEDIUMINT.BIGINT. CHAR 和 VARCHAR 的区别: CHAR 的长度是固定的,而 VARCHAR 的长度是可以变化 ...

  5. Selenium入门12 鼠标和键盘事件

    1 鼠标 集成在webdriver.ActionChains.单击.双击.右击.拖放等等.   2 键盘 引入包from selenium.webdriver.common.keys import K ...

  6. when 让你跳出异步回调噩梦 node.js下promise/A规范的使用

    其实关于promise 的博客,前端时间专门写了一篇关于 promise 规范的文章,promise规范 让 javascript 中的异步调用更加人性化. 简单回忆下: promise/A规范定义的 ...

  7. an exception occurred while initializing the database.

    对于手动删除本地的LocalDB数据库之后出现标题所示异常的,推荐下面的命令: sqllocaldb.exe stop v11.0 sqllocaldb.exe delete v11.0 在程序包管理 ...

  8. 剑指offer 和为s的两个数字的调试

    这是整个调试, for (int i:s) cout<<i<<endl;这句话是c++11特性下的一种遍历方式 在编译的时候需要加-std=c++11,即g++ 41.cpp ...

  9. Java解析Excel工具类(兼容xls和xlsx)

    依赖jar <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml&l ...

  10. ROS机器人程序设计

      在<ROS机器人程序设计>中,在第二章创建节点时给出一个接收和发送的例子,但是按照书中步骤编译时,遇到按个三个问题,现在罗列出来解决方案供参考. 建议在工作空间直接输入 catkin_ ...