-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leetcode0133.cpp
66 lines (53 loc) · 1.1 KB
/
Leetcode0133.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
#include <bits/stdc++.h>
using namespace std;
//Filename: Leetcode133.cpp
//
//Author: dezhonger - csdisassembly@gmail.com
//Create: 2021-02-26 00:06:58
class Node {
public:
int val;
vector<Node*> neighbors;
Node() {
val = 0;
neighbors = vector<Node*>();
}
Node(int _val) {
val = _val;
neighbors = vector<Node*>();
}
Node(int _val, vector<Node*> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
class Solution {
public:
unordered_map<Node*, Node*> hash;
Node* cloneGraph(Node* node) {
if (!node) return NULL;
//copy points
dfs(node);
//copy edge
dfs2();
return hash[node];
}
void dfs(Node* node) {
hash[node] = new Node(node -> val);
for (auto ver: node -> neighbors) {
if (hash.count(ver) == 0) dfs(ver);
}
}
void dfs2() {
for (auto [s, d]: hash) {
for (auto ver: s -> neighbors) {
d -> neighbors.push_back(hash[ver]);
}
}
}
};
int main() {
ios::sync_with_stdio (false);
cin.tie (0);
return 0;
}