Close
Topic list
Clone Graph
MEDIUM
25 mins
Graph
Topics (Covered in this problem)
Problem solved
Skill meter
Graph
-
-
Other topics
Problem solved
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

# Clone Graph

Contributed by
Yash_5830
Medium
0/80
Avg time to solve 25 mins
Success Rate 75 %
Share

## Problem Statement

#### The structure of the graphNode class is as follows:

``````class graphNode
{
public:
int data;
vector<graphNode*> neighbours;
}
``````
##### Note :
``````1. Nodes are numbered from 1 to N.

2. Your solution will run on multiple test cases. If you are using global variables make sure to clear them.
``````
Detailed explanation ( Input/output format, Notes, Constraints, Images )
##### Sample Input 1 :
``````2
5
6
1 2
4 1
2 4
3 4
5 2
1 3
3
2
1 2
1 3
``````
##### Sample Output 1 :
``````true
true
``````
##### Explanation of Input 1 :
``````In the first test case, the returned graph contains 5 nodes and 6 edges which are:
1 2
4 1
2 4
3 4
5 2
1 3
Since it is similar to the given graph with different address nodes then the solution is correct.

In the second test case, the returned graph contains 3 nodes and 2 edges which are:
1 2
1 3
Since it is similar to the given graph with different address nodes then the solution is correct.
``````
##### Sample Input 2 :
``````2
5
4
1 2
2 3
3 4
4 5
2
1
1 2
``````
##### Sample Output 2 :
``````true
true
``````
##### Explanation of Input 2 :
``````In the first test case, the returned graph contains 5 nodes and 4 edges which are:
1 2
2 3
3 4
4 5
Since it is similar to the given graph with different address nodes then the solution is correct.

In the second test case, the returned graph contains 2 nodes and 1 edge which is:
1 2

Since it is similar to the given graph with different address nodes then the solution is correct.
``````
Auto
Console