-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path133_Clone_Graph.cpp
88 lines (85 loc) · 2.23 KB
/
133_Clone_Graph.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include"struct_define.h"
#include<iostream>
#include<vector>
#include<string>
#include<map>
using namespace std;
static const auto x=[](){
std::ios::sync_with_stdio(false);
std:cin.tie(nullptr);
return nullptr;
}();
class Solution {
map<int, vector<int> > node_to_neighbors;
map<int, UndirectedGraphNode*> node_to_nodeptr;
public:
void show_edge_list()
{
for(auto e : node_to_neighbors)
{
cout<<"edge "<<e.first<<" -> \n";
for(auto ee : e.second)
{
cout<<"\t"<<ee<<endl;
}
cout<<endl;
}
cout<<endl<<endl;
}
void get_edge_list(UndirectedGraphNode *node)
{
if( node == nullptr) return;
int key = node->label;
if( node_to_neighbors.find(key) == node_to_neighbors.end())
{
vector<int> temp;
for(auto e : node->neighbors)
{
temp.push_back(e->label);
}
node_to_neighbors[key]=temp;
for(auto e : node->neighbors)
{
get_edge_list(e);
}
}
}
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node)
{
if( node == nullptr)
return nullptr;
get_edge_list(node);
for(auto e :node_to_neighbors)
{
UndirectedGraphNode* node = new UndirectedGraphNode(e.first);
node_to_nodeptr[e.first] = node;
}
for(auto e : node_to_neighbors)
{
for(auto ee : e.second)
{
node_to_nodeptr[e.first]->neighbors.push_back(node_to_nodeptr[ee]);
}
}
return node_to_nodeptr[node->label];
}
};
int main(int argc, char const *argv[])
{
UndirectedGraphNode n5(5);
UndirectedGraphNode n1(1);
UndirectedGraphNode n2(2);
UndirectedGraphNode n3(3);
UndirectedGraphNode n4(4);
n4.neighbors={&n5};
n2.neighbors={&n4};
n3.neighbors={&n4};
n1.neighbors={&n2, &n3};
Solution sol;
UndirectedGraphNode* nn = sol.cloneGraph(&n1);
sol.show_edge_list();
Solution sol2;
sol2.get_edge_list(nn);
sol2.show_edge_list();
return 0;
}