题目:

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. OSS基本概念介绍

    存储空间(Bucket): 存储空间是用于存储对象(Object)的容器,所有的对象都必须隶属于某个存储空间. 可以设置和修改存储空间属性用来控制地域.访问权限.生命周期等,这些属性设置直接作用于该存 ...

  2. Unity中的输入

    目录 移动平台的输入 触摸 触摸相关的函数 触摸的一个示例 重力加速器 在Unity中访问重力加速器的信息 重力加速器示例 虚拟键盘 其他输入 传统的输入 鼠标,键盘,控制杆,手柄 虚拟控制轴(Vir ...

  3. MySQL-数据类型及选择

    一.数据类型 详见:http://www.runoob.com/mysql/mysql-data-types.html 二.类型选择 整形>date,time>enum,char>v ...

  4. intelij idea相关笔记--持续更新

    一.快捷键: Ctrl+F 文件内查找 Ctrl+Shift+F 全局查找 Ctrl+Shift+N 查找文件 Ctrl+Alt+← 返回上一步 Ctrl+Alt+→ 返回下一步 二.编译相关: 如果 ...

  5. Python Visual Studio 2015

    对于一直是C#开发的我来说,上Python是老早就想的事情了. 上次有个项目开始做就说要用Python,后来因为不太熟练就给推掉了.现在终于还是有机会开始下Python之旅. 因为是在Visual S ...

  6. c++输入

    1. char c = getchar(); 输入单个字符,可输入空格.换行符. 2. cin >> s; 不读取空格或换行符. 3. getline(cin, s); 输入一行到字符串s ...

  7. 如何在SAP Cloud for Customer自定义BO中创建访问控制

    文章作者: Yi 已获得Yi的转载许可. 访问控制方式和使用注意事项 1. C4C中的访问控制有两种方式 RelevantForAccessControl AccessControlContext 2 ...

  8. Using an Image for the Layer’s Content

    Using an Image for the Layer’s Content Because a layer is just a container for managing a bitmap ima ...

  9. Linux系统运维常见面试简答题(36题)

    1.请描述下linux 系统的开机启动过程开机加电BIOS自检———–>MBR引导———–>grub引导菜单———–>加载内核———–>启动init进程———–>读取in ...

  10. 2018.6.12 Oracle问题

    ORA-01950: 对表空间 'USERS' 无权限 创建新的用户时,要指定default tablespace,否则它会把system表空间当成自己的缺省表空间.这样做是不提倡的.估计原来创建某个 ...