Skip to content
Programmingoneonone
Programmingoneonone
  • 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
Programmingoneonone

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 solutions

Post navigation

Previous post
Next post

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
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes