-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path133.cpp
More file actions
37 lines (35 loc) · 893 Bytes
/
133.cpp
File metadata and controls
37 lines (35 loc) · 893 Bytes
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
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
using namespace std;
struct UndirectedGraphNode {
int label;
vector<UndirectedGraphNode *> neighbors;
UndirectedGraphNode(int x) : label(x) {};
};
class Solution {
public:
map<UndirectedGraphNode*,UndirectedGraphNode*> mp;
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if(node == NULL)
return NULL;
if(mp.find(node) != mp.end())
return mp[node];
UndirectedGraphNode * cloneNode = new UndirectedGraphNode(node->label);
mp[node] = cloneNode;
for(int i = 0; i < node->neighbors.size(); i++) {
UndirectedGraphNode *temp = cloneGraph(node->neighbors[i]);
if(temp != NULL)
cloneNode->neighbors.push_back(temp);
}
return cloneNode;
}
};