题目:

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.

OJ's undirected graph serialization:

Nodes are labeled uniquely.

We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.

As an example, consider the serialized graph {0,1,2#1,2#2,2}.

The graph has a total of three nodes, and therefore contains three parts as separated by #.

  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1. Connect node 1 to node 2.
  3. Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.

Visually, the graph looks like the following:

       1
/ \
/ \
0 --- 2
/ \
\_/

解题思路:

本题要解决的问题就是对一个图进行clone,这不禁让我们想起图的遍历:DFS和BFS,所以我们可以再遍历图中某个点时,增加一些附加的操作而不仅仅是访问它,这里我们要增加的附加操作即对该点进行clone。
现在我们利用DFS和BFS两种方式进行解题。

实现代码:

#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
using namespace std; /*
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization:
Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.
As an example, consider the serialized graph {0,1,2#1,2#2,2}. The graph has a total of three nodes, and therefore contains three parts as separated by #. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
Second node is labeled as 1. Connect node 1 to node 2.
Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.
Visually, the graph looks like the following: 1
/ \
/ \
0 --- 2
/ \
\_/ */ struct UndirectedGraphNode {
int label;
vector<UndirectedGraphNode *> neighbors;
UndirectedGraphNode(int x) : label(x) {};
}; class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if(node == NULL)
return NULL;
unordered_map<int, UndirectedGraphNode*> hashtable;//标志位,是否已拷贝了key为label,value为新拷贝的node
return dfsclone(node, hashtable); } //DFS
UndirectedGraphNode *dfsclone(UndirectedGraphNode *node, unordered_map<int, UndirectedGraphNode*> &hashtable)
{
if(node == NULL)
return NULL;
if(hashtable.count(node->label) > )
return hashtable[node->label];//已经拷贝好了,则直接返回即可,否则进行下面的拷贝操作
UndirectedGraphNode *copyNode = new UndirectedGraphNode(node->label);
hashtable[copyNode->label] = copyNode;
vector<UndirectedGraphNode *>::iterator iter;
for(iter = node->neighbors.begin(); iter != node->neighbors.end(); ++iter)
{
copyNode->neighbors.push_back(dfsclone(*iter, hashtable));//进行深度优先算法拷贝
} return copyNode;
} //BFS
UndirectedGraphNode *bfsclone(UndirectedGraphNode *node, unordered_map<int, UndirectedGraphNode*> &hashtable)
{
if(node == NULL)
return NULL;
queue<UndirectedGraphNode *> qu;
UndirectedGraphNode *copyNode = new UndirectedGraphNode(node->label);
hashtable[copyNode->label] = copyNode;
qu.push(node);
while(!qu.empty())
{
UndirectedGraphNode *tnode = qu.front();
qu.pop();
vector<UndirectedGraphNode *>::iterator iter;
for(iter = node->neighbors.begin(); iter != node->neighbors.end(); ++iter)
{
if(hashtable.count((*iter)->label) == )
{
UndirectedGraphNode *tnode = new UndirectedGraphNode((*iter)->label);
hashtable[(*iter)->label] = tnode;
qu.push(*iter);
}
(hashtable[tnode->label])->neighbors.push_back(hashtable[(*iter)->label]); } }
return copyNode;
}
};
int main(void)
{
return ;
}

LeetCode133:Clone Graph的更多相关文章

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

  2. 21. Clone Graph

    Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...

  3. 133. Clone Graph

    题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...

  4. [LeetCode]Copy List with Random Pointer &amp;Clone Graph 复杂链表的复制&amp;图的复制

    /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...

  5. 【LeetCode】133. Clone Graph (3 solutions)

    Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...

  6. LeetCode: Clone Graph 解题报告

    Clone GraphClone an undirected graph. Each node in the graph contains a label and a list of its neig ...

  7. [Leetcode Week3]Clone Graph

    Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...

  8. 【Clone Graph】cpp

    题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...

  9. 【Lintcode】137.Clone Graph

    题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...

随机推荐

  1. 开启mongod服务(Mongo运行错误:Failed to connect 127.0.0.1:27017,reason:errno:10061由于目标计算机积极拒绝,无法连接)

    问题:Mongo运行错误:Failed to connect 127.0.0.1:27017,reason:errno:10061由于目标计算机积极拒绝,无法连接 在Mongodb的安装过程中碰到的问 ...

  2. runloop - 介绍

    1. 简介 没有的话 有的话

  3. 10-能不能组成m

    /*                                找数达人 时间限制:1000 ms  |  内存限制:65535 KB      难度:2 描述 小明最近做出了一道题:如何在一组数 ...

  4. CentOS设置yum存储库 (nginx)

    要为RHEL / CentOS设置yum存储库,请创建/etc/yum.repos.d/nginx.repo 使用以下内容命名的文件 : [nginx] name=nginx repo baseurl ...

  5. Debian9开机运行Python脚本

    吾星喵 关注 2018.04.14 15:30 字数 214 阅读 202评论 0喜欢 1 Debian9开机运行Python脚本 Debian 9.x "stretch" 解决 ...

  6. 用Vue-cli生成vue+webpack的项目模板怎么设置为vue1.0版本?

    用Vue-cli生成vue+webpack的项目模板 $ npm install -g vue-cli $ vue init webpack my-project $ cd my-project $ ...

  7. Confluence无法打开编辑器,一直在转圈

    在管理员界面中,将Collaborative editing 设置为Off 或者 Limited . 快速找到该界面的方式是,在搜索框里搜索 “Collaborative editing”. 折腾了几 ...

  8. Selenium安装中的一些问题及解决办法-软硕1703班3组整理分享

    非常感谢软件工程硕士1703班3组同学的热心,他们将安装Selenium过程中踩过的坑替大家填上了.希望还没有来得及踩坑的,或者掉进坑里还没爬出来的小组,能顺利跨过去这个安装的坑. 如下是原文. Se ...

  9. 2018.09.23 codeforces 1053A. In Search of an Easy Problem(gcd)

    传送门 今天的签到题. 有一个很显然的结论,gcd(n∗m,k)≤2gcd(n*m,k)\le 2gcd(n∗m,k)≤2. 本蒟蒻是用的行列式求三角形面积证明的. 如果满足这个条件,就可以直接构造出 ...

  10. 2018.09.18 atcoder Best Representation(kmp)

    传送门 思路简单不知为何调试了很久. 显然要么分成n个(所有字符相同),要么分成1个(原字符串无循环节),要么分成两个(有长度至少为2的循环节). 一开始以为可以直接hash搞定. 后来wa了几次之后 ...