-
Notifications
You must be signed in to change notification settings - Fork 0
/
133-CloneGraph.cpp
69 lines (59 loc) · 2.18 KB
/
133-CloneGraph.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*=============================================================================
# FileName: 133-CloneGraph.cpp
# Desc:
# Author: qsword
# Email: huangjian1993@gmail.com
# HomePage:
# Created: 2015-05-09 22:13:00
# Version: 0.0.1
# LastChange: 2015-05-09 23:06:34
# History:
# 0.0.1 | qsword | init
=============================================================================*/
#include <stdio.h>
#include <vector>
#include <map>
#include <queue>
using namespace std;
struct UndirectedGraphNode {
int label;
vector<UndirectedGraphNode *> neighbors;
UndirectedGraphNode(int x) : label(x) {};
};
class Solution {
public:
void dfs(UndirectedGraphNode *root, UndirectedGraphNode *node, map<int, UndirectedGraphNode *> &node_map) {
for (int i = 0; i < node->neighbors.size(); i ++) {
root->neighbors.push_back(node_map[node->neighbors[i]->label]);
if (root->neighbors[i]->neighbors.empty() && !(node->neighbors[i]->neighbors.empty()) && root->neighbors[i] != root) {
dfs(root->neighbors[i], node->neighbors[i], node_map);
}
}
return ;
}
//82ms
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (!node) {
return node;
}
queue<UndirectedGraphNode *> node_queue;
map<int, UndirectedGraphNode *> node_map;
node_queue.push(node);
while (!node_queue.empty()) {
UndirectedGraphNode *temp = node_queue.front();
node_queue.pop();
if (node_map.find(temp->label) == node_map.end()) {
UndirectedGraphNode *new_node = new UndirectedGraphNode(temp->label);
node_map.insert(pair<int, UndirectedGraphNode*>(new_node->label, new_node));
for (int i = 0; i < temp->neighbors.size(); i ++) {
if (temp != temp->neighbors[i]) {
node_queue.push(temp->neighbors[i]);
}
}
}
}
UndirectedGraphNode *root = node_map[node->label];
dfs(root, node, node_map);
return root;
}
};