题目:

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. cocos2d-x 学习资料汇总

    cocos2d-x配置问题 - 我要飞的更高 - 博客频道 - CSDN.NET Cocos2d-x win7 + vs2010 配置图文详解(亲测) - 子龙山人 - 博客园 WINDONWS7+V ...

  2. DB2数据库常用语句

    1.快速清空大量数据表数据,但是还原不了 alter table rm_customer activate not logged initially with empty table2.大量导出表语句 ...

  3. python3基础08(exec、bytearray使用等)

    #!/usr/bin/env python# -*- coding:utf-8 -*- str="test"print(ascii(str))a=bytearray("a ...

  4. Shuffle Cards

    C: Shuffle Cards 时间限制: 1 Sec  内存限制: 128 MB提交: 3  解决: 3[提交] [状态] [讨论版] [命题人:admin] 题目描述 Eddy likes to ...

  5. 2018.1.30 PHP编程之验证码

    PHP编程之验证码 1.创建验证码函数 验证码函数输入通用函数,将函数放入global.func.php里. //创建一个随机码 for($ i=0;$i<4;$i++){ $_nmsg. = ...

  6. kubernetes-配置管理(十一)

    Secret https://kubernetes.io/docs/concepts/configuration/secret/ Secret解决了密码.token.密钥等敏感数据的配置问题,而不需要 ...

  7. c++ question 003 求两数大者?

    #include <iostream>using namespace std; int main(){ //求两数中的大者? int a,b; cin>>a>>b; ...

  8. API调用微信getWXACodeUnlimit()获取小程序码

    微信文档地址:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/qr-code/getWXACodeUnlimit.html? ...

  9. DevOps - 配置管理 - Puppet

    uppet总结 一.基础知识 1. Puppet是开源的基于Ruby的系统配置管理工具,依赖于C/S的部署架构.Puppet这样的自动化配置管理工具可以帮助系统管理员更加方便的完成多台服务器的升级软件 ...

  10. 3D全景漫游

    全景图共分为三种: ①球面全景图 利用一张全景图围成一个球,自身位置位于球体内.由于图片是矩形,所以最上和最下的缝合处很明显就能够看得出来. 球面全景图是最接近人眼的构建模式,若利用多个立面构建,拼接 ...