Header Ad

HackerEarth Monk in the real estate problem solution

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

Post a Comment

0 Comments