class Solution {
public:
unordered_map<UndirectedGraphNode *,UndirectedGraphNode *> created;///although it takes O(n) space but it is the efficent way to search available in C++11...for C++4.8 use map...
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if(!node)return node; //not required ..only to be in safe side
if(created.find(node)!=created.end())return created[node]; ///if this node is already created then just return the reference of the new node created earlier
UndirectedGraphNode * t=new UndirectedGraphNode(node->label);///otherwise create a new node and mark corresponding node in original graph created.
created[node]=t;
for(int i=0;i<node->neighbors.size();i++){
t->neighbors.push_back(cloneGraph(node->neighbors[i]));//do recursively for all its neighbors...:)
}
return t;
}
};

I of cause know the code can work here. But the writer might not know it. “So the correctness of your code is coincidence.” The correctness depends on this problem which doesn't break the hack usage. A lot of scenarios will break the usage. If the writer knows the inside of that code, as i mentioned "Even it is by design, I don't recommend it."