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

HackerEarth Maximum size of a set problem solution

YASH PAL, 31 July 202417 February 2026
In this HackerEarth Maximum size of a set problem solution You are given a DAG N with N nodes and M edges. You are building a graph G^. G^ contains the same vertex set as G and all edges are available in G. Moreover,
  1. If there exists an edge between vertex u and v in G, then there does not exist an edge between vertex v and u in G^.
  2. If there exists an edge between vertex u and v in G and also between v and w in G, then there exists an edge between vertex u and w in G^.
For G^, find the maximum possible size of S where S is a set of vertices in G^ such that there exists an edge between every unordered pair of vertex present in S.
 
The meaning of unordered is that there must exist an edge between every pair of vertex (u,v), that is, either u – > v or v – > u must be in an edge set.
 
 
HackerEarth Maximum size of a set problem solution

 

 

HackerEarth Maximum size of a set problem solution.

#include<bits/stdc++.h>
#define ll long long int
#define MAX 200005
using namespace std;
vector<int>adjacent[MAX];
int dp[MAX];
bool visited[MAX];

void dfs(int u)
{
visited[u] = true;

dp[u] = 1;
for(int i = 0 ; i < adjacent[u].size() ; i++)
{
int v = adjacent[u][i];

if(!visited[v])
{
dfs(v);
}

dp[u] = max(dp[u], 1 + dp[v]);
}
}

int main(){
int N, M;
cin>>N>>M;

assert(N <= 200000 && M <= 1000000);

for(int i = 1 ; i <= M ; i++)
{
int u , v;
cin >> u >> v;
adjacent[u].push_back(v);
}


for(int i = 1 ; i <= N ; i++)
{
if(!visited[i])
{
dfs(i);
}
}

int ans = -1;

for(int i = 1 ; i <= N ; i++)
{
ans = max(ans, dp[i]);
}

cout<<ans<<endl;
}
 

Second solution

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 14;
vector<int> g[maxn], ord;
int n, m, dp[maxn];
bool mark[maxn];
void dfs(int v){
if(mark[v])
return ;
mark[v] = true;
for(auto u : g[v])
dfs(u);
ord.push_back(v);
}
int main(){
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++){
int v, u;
cin >> v >> u;
v--, u--;
g[v].push_back(u);
}
for(int i = 0; i < n; i++)
dfs(i);
for(auto v : ord){
dp[v] = 1;
for(auto u : g[v])
dp[v] = max(dp[v], dp[u] + 1);
}
cout << *max_element(dp, dp + n) << 'n';
}
 
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 *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

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