Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Monk in the real estate problem solution

YASH PAL, 31 July 202411 February 2026
In this HackerEarth Monk in the real estate problem solution, The Monk wants to buy some cities. To buy two cities, he needs to buy the road connecting those two cities. Now, you are given a list of roads, bought by the Monk. You need to tell how many cities did the Monk buy.
HackerEarth Monk in the real estate problem solution

HackerEarth Monk in the real estate problem solution.

#include <iostream>
#include <cassert>

using namespace std;
const int MAX = 1e4 + 5;
bool visited[MAX];

int main()
{
int edges, x, y, testcases, nodes;
cin >> testcases;
assert(1 <= testcases and testcases <= 100);
while(testcases--)
{
cin >> edges;
assert(1 <= edges and edges <= 1000);
for(int i = 0;i < edges;++i)
{
cin >> x >> y;
assert(1 <= x and x <= 10000);
assert(1 <= y and y <= 10000);
visited[x] = true;
visited[y] = true;
}
nodes = 0;
for(int i = 0;i < MAX;++i)
if(visited[i])
{
visited[i] = false;
nodes++;
}
cout << nodes << endl;
}
return 0;
}

Second solution

tc = int(raw_input())
assert(tc>0 and tc<101)
while tc>0:
tc = tc - 1
n = int(raw_input())
assert(n>0 and n<1001)
visited = [False]*10013
for i in xrange(n):
a, b = map(int, raw_input().split())
assert(a>0 and a<10001)
assert(b>0 and b<10001)
visited[a] = True
visited[b] = True
nodes = 0
for i in xrange(10013):
if visited[i]:
visited[i] = False
nodes += 1
print nodes
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes