题目:

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
/ \
\_/

代码:

/**
* Definition for undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/
class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node)
{
map<UndirectedGraphNode *, UndirectedGraphNode *> copied;
return Solution::dfs(copied, node);
}
static UndirectedGraphNode *dfs(
map<UndirectedGraphNode *, UndirectedGraphNode *>& copied,
UndirectedGraphNode *node)
{
if ( node == NULL) return NULL;
if ( copied.find(node)!=copied.end() ) return copied[node];
UndirectedGraphNode *cloneNode = new UndirectedGraphNode(node->label);
copied[node] = cloneNode;
for ( int i=; i<node->neighbors.size(); ++i )
{
cloneNode->neighbors.push_back( Solution::dfs(copied,node->neighbors[i]) );
}
return cloneNode;
}
};

tips:

图的深拷贝。

学到的一个技巧是如何不重复拷贝图中的node:用一个map<node *, node *>记录已经拷贝过的原图中的点以及其对应的新图中的点。

剩下的就是按照深搜模板来完成。

====================================

第二次过这道题,照着之前的思路写,漏掉了重要的红字的部分。

/**
* Definition for undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/
class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node)
{
map<UndirectedGraphNode*, UndirectedGraphNode*> originCopy;
return Solution::dfs(node, originCopy);
}
static UndirectedGraphNode* dfs(
UndirectedGraphNode* origin,
map<UndirectedGraphNode*, UndirectedGraphNode*>& originCopy)
{
if ( !origin ) return NULL;
if ( originCopy.find(origin)!=originCopy.end() ) return originCopy[origin];
UndirectedGraphNode* copy = new UndirectedGraphNode(origin->label);
originCopy[origin] = copy;
for ( int i=; i<origin->neighbors.size(); ++i )
{
copy->neighbors.push_back(Solution::dfs(origin->neighbors[i], originCopy));
}
return copy;
}
};

【Clone Graph】cpp的更多相关文章

  1. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  2. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  3. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  4. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  5. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  6. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  7. 【Sort List】cpp

    题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...

  8. 【Path Sum】cpp

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  9. 【Symmetric Tree】cpp

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

随机推荐

  1. 微信小程序开发入门首选

    推荐一本书吧,直接上图,微信开发,微信网页开发,微信小程序开发,都用得着. 推荐一本书吧,直接上图,微信开发,微信网页开发,微信小程序开发,都用得着. 推荐一本书吧,直接上图,微信开发,微信网页开发, ...

  2. php编译安装过程中遇到问题

    编译安装PHP时遇到的问题 问题1: configure: error: xml2-config not found. Please check your libxml2 installation. ...

  3. LeetCode Sort List 链表排序(规定 O(nlogn) )

    Status: AcceptedRuntime: 66 ms 题意:根据给出的单链表,用O(nlogn)的时间复杂度来排序.由时间复杂度想到快排.归并这两种排序.本次用的是归并排序.递归将链表的规模不 ...

  4. iphone开发笔记

    1.uiimage图片拉伸 - (void)stretchBackgroundImage { //UIImage *originalImage = [[self backgroundImageForS ...

  5. Head First Python 读书笔记

    记录一下这段时间看<Head First Python>记录的一些小知识,只是记了很少一部分,有需要的话以后再添加吧. for循环的使用: for 目标标识符 in 列表: 处理代码 if ...

  6. linux 命令——7 mv(转)

    mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. 1.命令格式: mv [选项] 源文件或目 ...

  7. Python实现注册和登录

    一.注册账号需要实现的功能 1.输入:用户名,密码,密码确认 2.限制1:输入的账号和密码不能为空 3.限制2:两次输入密码必须一致 4.限制3:用户名不能重复 5.限制4:错误次数为4次 6.用字典 ...

  8. 输入hostname -f提示:hostname: Unknown host

    解决方法:将/etc/hosts文件中的内容添加如下所示 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdo ...

  9. js实现全选,反选,全不选

    思路:1.获取元素.2.用for循环历遍数组,把checkbox的checked设置为true即实现全选,把checkbox的checked设置为false即实现不选.3.通过if判断,如果check ...

  10. 问题002:我们要使用的Java是哪个版本的?什么是JVM、JRE、JDK、IDE、API?

    三个版本:1.java SE 标准版 2.java EE企业版 3.Java ME 小型版本 JVM (java virtual machine) java虚拟机 JRE(java runtime e ...