Login

All Problems

Problem title

Difficulty

Avg time to solve

Diagonal Traversal of a binary tree.

Easy

10 mins

DFS Traversal

Moderate

35 mins

Smallest Window

Moderate

10 mins

Permutations of a String

Moderate

15 mins

Maze with N doors and 1 Key

Moderate

15 mins

Sub Query Sum

Easy

10 mins

Sort Array

Moderate

15 mins

Find Kth row of Pascal's Triangle

Easy

15 mins

Internet Address

Easy

15 mins

Optimal Strategy for a Game

Easy

15 mins

Problem

Submissions

4

Avg. time to solve

35 min

Success Rate

65%

Problem Statement

```
V is the number of vertices present in graph G and vertices are numbered from 0 to V-1.
E is the number of edges present in graph G.
```

```
The Graph may not be connected i.e there may exist multiple components in a graph.
```

```
The first line of input will contain two Integers V and E, separated by a single space.
From the second line onwards, the next 'E' lines will denote the undirected edge of the graphs.
Every edge is defined by two single space-separated integers 'a' and 'b', which signifies an edge between the vertices 'a' and 'b'.
```

```
The first line of output will contain the size of the connected components.
For every connected component in the graph, print the vertices of the component in the sorted order of the vertex values separated with a single space.
Print each component in on a different line by making sure that the first vertex of each component is also sorted on the vertex values.
A component having a smaller first vertex in sorted order will come first.
```

```
2 <= V <= 10^3
1 <= E <= (5 * (10^3))
Time Limit: 1sec
```

```
5 4
0 2
0 1
1 2
3 4
```

```
2
0 1 2
3 4
```

```
If we do a DFS traversal from vertex 0 we will get a component with vertices [0, 2, 1]. If we do a DFS traversal from 3 we will get another component with vertices [3, 4]
Hence, we have two disconnected components so on the first line, print 2. Now, print each component in increasing order. On the first line print 0 1 2 and on the second line, print 3 4.
[0 1 2] comes before [3 4] as the first vertex 0 from the first component is smaller than the first vertex 3 from the second component.
```

```
9 7
0 1
0 2
0 5
3 6
7 4
4 8
7 8
```

```
3
0 1 2 5
3 6
4 7 8
```

Console

Sample Test Case

Custom Test Case

Download Test Cases

Test Case 1

Test Case 2

Test Case 3

Saving Code...

Full Screen Mode

Change Language

Change Theme

Solution submission not allowed

Save Code

Reset Code