Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • 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
Programming101
Programming101

Learn everything about programming

HackerEarth Monk in the real estate problem solution

YASH PAL, 31 July 2024
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

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes