[Problem] Dinner with GF

My girl friend is interested to buy some cities. Inorder to buy two cities, she needs to buy the road connecting those two cities. She is concentrating on only the roads not cities as she has lot of other work.

My bad luck,  same time I invited her for dinner at Greenpark.

She said invitation will be accepted when I give number of cities count which she bought.

Now, I was given a list of roads, bought by my GF.  I need to tell her how many cities did she buy.

Input:
First line contains an integer T, denoting the number of test cases. The first line of each test case contains an integer E, denoting the number of roads. The next E lines contain two space separated integers X and Y, denoting that there is an road between city X and city Y.

Output:
For each test case, I need to print the number of cities she bought.

Constraint:
1 <= T <= 100
1 <= E <= 1000
1 <= X, Y <= 10000

Let me solve this with Graph representation.
 Key points: Each city will be vertex of the graph.
                    Road will be the edge between the vertex.
   


As I was given the number of roads which  are connecting to the cities.
It will be easy If I find the unique vertices in a graph with given edges.

For this I can use Set implemented classes because It won't allow us to put duplicate values.
Solution:


Source:https://www.hackerearth.com

No comments:

Post a Comment