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. PHP:substr和mb_substr的区别

    substr和mb_substr函数都是获取字符串中的某个部分 那么,它们的区别在哪儿呢? 区别: substr :全部是英语.数字就正常:但有一些的字元是占用多个位元的,substr()就得不到你预 ...

  2. 少年开始学习c#编程,过路的大神请担待!

    这应该真正算开始学习编程, 安装好了 linux, 开通了博客员的博客, 同时今天也需要把sublime安好, 安装MonoDevelop Codeblocks mysql-workbench 配置好 ...

  3. nbu8.1配置群集SQL Server实例的备份

    1.About SQL Server high availability (HA) environments SQL Server Intelligent policies support the f ...

  4. 【转】VMware虚拟机系统无法上网怎么办?

    有很多用户通过安装VMware软件来创建虚拟机系统,其中就有部分用户在创建好虚拟机系统后遇到无法上网的问题,下面PC6苹果网小编就给大家带来VMware虚拟机系统下无法上网的解决办法: 1.在虚拟机右 ...

  5. 用蒙特卡洛方法计算派-python和R语言

    用蒙特卡洛方法算pi-基于python和R语言 最近follow了MOOC上一门python课,开始学Python.同时,买来了概率论与数理统计,准备自学一下统计.(因为被鄙视过不是统计专业却想搞数据 ...

  6. Swiper插件

    中文官网:Swiper中文网 英文:英文网 此插件功能比较强大,网页端.手机端都可以使用的插件.这里记录一下在微信h5页面里面滑动获取数据. 先下载css和js,引用到项目中 这里有6个节点,没划一个 ...

  7. React后台管理系统-订单管理

    1.订单管理页面和商品管理页面类似,都是一个搜索组件+列表组件 2.搜索框search组件 import React from 'react';   class ListSearch extends ...

  8. IE 兼容模式 设置 Meta Compatible 和 Iframe 子页面的关系

    背景 因为历史原因,之前很多的系统都会是 顶级页面+Iframe来加载子级页面的这种模式构件系统,而且系统都只能运行在IE6或者IE 高版本兼容模式下(IE 7模式). 随着现在的审美原来越高,脚本能 ...

  9. 知识总结和记录——Bootstrap

    官方地址:https://getbootstrap.com 中文地址:http://www.bootcss.com/ 使用V3版本的Bootstrap,下载的是用于生产环境的Bootstrap. 目录 ...

  10. SpringBoot学习10:springboot整合mybatis

    需求:通过使用 SpringBoot+SpringMVC+MyBatis 整合实现一个对数据库中的 t_user 表的 CRUD 的操作 1.创建maven项目,添加项目所需依赖 <!--spr ...