题目:

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. Bugzilla-5.0.3 (OpenLogic CentOS 7.2)

    平台: CentOS 类型: 虚拟机镜像 软件包: apache2.4.6 bugzilla5.0.3 mariadb5.5.47 perl5.16.3 apache bug tracking bug ...

  2. gitlab用户添加ssh免密钥认证后clone还是要求输入密码

    今天在centos 7公网服务器上安装gitlab在配置ssh免密钥时遇到一个奇怪的事,正确添加了本机的公钥到gitlab账户上,进行clone时死活都要你输入密码gitlab使用yum安装的,之前在 ...

  3. spark集群配置细则总结

    修改目录与目录组: sudo chown -R hadoop:hadoop spark-1.6.1-bin-hadoop2.6 sudo chown -R hadoop:hadoop jdk1.8.0 ...

  4. 关于前端的交互 ajax

    对于交互来说,可以利用原生的javascript和jquery 这篇说的就是jquery 1 不是跨域的 利用$ajax({})这个函数实现的 $.ajax({ url: "", ...

  5. Sql Server配置管理器与 Sql Server Management Studio

    起初只安装了Sql Server配置管理器,之后用Navicat连接,总是报错(命名管道提供程序: 无法打开与 SQL Server 的连接 [53]) (另记:Navicat的“主机名或IP地址:” ...

  6. IOS AppDelegate常用方法

    // 当应用程序启动完毕的时候就会调用(系统自动调用) - (BOOL)application:(UIApplication *)application didFinishLaunchingWithO ...

  7. selenium 使用键盘时 提示java.lang.IllegalArgumentException: Key Down / Up events only make sense for modifier keys.

    输入某个内容后,使用enter键进行确认,最开始使用方式为: driver.findElement(By.xpath("//input[@name='supplier_name'][@id= ...

  8. 集成Ehcache用来缓存表以后,怎么设置缓存刷新时间

    问答 集成Ehcache用来缓存表以后,怎么设置缓存刷新时间  发布于 217天前  作者 老司机  93 次浏览  复制  上一个帖子  下一个帖子  标签: 无 集成Ehcache用来缓存表以后, ...

  9. python实现剑指offer对称的二叉树

    题目描述 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. # -*- coding:utf-8 -*- # class TreeNode ...

  10. html5shiv.js的作用是

    解析 html5shiv主要解决HTML5提出的新的元素不被IE6-8识别,这些新元素不能作为父节点包裹子元素,并且不能应用CSS样式.让CSS 样式应用在未知元素上只需执行 document.cre ...