题目:

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. 构建第一个Spring Boot2.0应用之application.properties和application.yml(八)

    本节学习在项目中配置文件配置的方式,一种是通过applicaiton.properties,一种是通过application.yml方式. 一.环境: IDE:IntelliJ IDEA 2017.1 ...

  2. php 实现格式化数字功能

    php 实现数字格式化功能 /** * @param $num 数字 * @param int $decimal 精度 * @param int $point_len 分隔位长度 * @return ...

  3. Wince 6.0获取设备的分辨率 自动设置窗体位置

    调用微软提供给wince的API  “coredll.dll” [DllImport("coredll.dll")] public static extern int GetSys ...

  4. Visual Studio 2015 Preview 使用中问题一枚

    只要碰到IO读写,文件不存在之类的系统异常,就会崩溃一下给你看看.直接重新VS. 不该有的问题确实存在着???? 正常情况是这样的 直接崩溃时万万不行的!!!!

  5. php使用GD库实现图片水印和缩略图——生成图片缩略图

    今天呢,就来学习一下在php中使用PD库来实现对图片水印的文字水印方法,不需要PS哦! 首先,准备素材 (1)准备一张图片 (2)准备一张水印(最好是透明的,即背景是白色底) (3)准备一中字体(在电 ...

  6. window下安装ubuntu(ubuntu可删除)

    进入ububtu13.04的安装界面,这里我们选择了“中文(简体)”,然后单击安装: 下图是现场拍的:   出现如下图时,请根据需要选择,然后单击“继续” , 接下来会出现问你是否要连接网络,我们选择 ...

  7. CPP-基础:C_C++变量命名规则

    C_C++变量命名规则 变量命名规则是为了增强代码的可读性和容易维护性.以下为C++必须遵守的变量命名规则: 1. 变量名只能是字母(A-Z,a-z)和数字(0-9)或者下划线(_)组成. 2. 第一 ...

  8. 私人定制,十款最佳Node.js MVC框架

    Node.js是JavaScript中最为流行的框架之一,易于创建可扩展的Web应用.本文分享十款最佳的JavaScript框架. Node.js是JavaScript中最为流行的框架之一,易于创建可 ...

  9. 通过jQuery遍历div里面的checkbox

    遍历: $('#queryUser2 input[type="checkbox"]:checked').each( function () { a = a + $(this).va ...

  10. 流行JAVA开发工具

    流行JAVA开发工具 正所谓工欲善其事必先利其器,我们在开发java语言过程中同样需要依款不错的开发工具,目前市场上的IDE很多,本文为大家推荐以下下几款java开发工具: Eclipse(推荐):另 ...