Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • 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
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerEarth A Walk to Remember problem solution

YASH PAL, 31 July 2024
In this HackerEarth A Walk to Remember problem solution Dilku was thinking about the first time he met his girl… It was indeed a walk to remember. The romantic weather and her silly talks. He was completely mesmarized. Those were the days!..
Today is his girl’s birthday and he wants to make it special for her. He wants to again take her on a “special walk” that they would remember for the lifetime.
The city in which Dilku lives is represented as an unweighted directed graph with N nodes and M edges. A “special walk” in the graph starting at node u is a simple path that begins and ends at the same node u.
Formally, A special walk is path u , a1 , a2 , a3 ,…, ai ,…. , u where ai are distinct and not equal to u for all i.
Now since Dilku is really nervous about taking his girl out, he needs your help. For every node in the given graph, tell whether it is possible for Dilku to take his girl on a “special walk” starting at that node.
HackerEarth A Walk to Remember problem solution

HackerEarth A Walk to Remember problem solution.

#include<bits/stdc++.h>

using namespace std;

typedef pair<int,int> II;
typedef vector< II > VII;
typedef vector<int> VI;
typedef vector< VI > VVI;
typedef long long int LL;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(),a.end()
#define SET(a,b) memset(a,b,sizeof(a))

#define si(n) scanf("%d",&n)
#define dout(n) printf("%dn",n)
#define sll(n) scanf("%lld",&n)
#define lldout(n) printf("%lldn",n)
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

//FILE *fin = freopen("in","r",stdin);
//FILE *fout = freopen("out","w",stdout);
const int N = int(1e5)+1;
const int M = int(2e5)+1;
VI g[N],rg[N],comp,order;
int vis[N],ans[N];
void dfs1(int u)
{
vis[u]=1;
for(int i=0;i<SZ(g[u]);i++)
if(!vis[g[u][i]])
dfs1(g[u][i]);
order.PB(u);
}
void dfs2(int u)
{
vis[u]=1;comp.PB(u);
for(int i=0;i<SZ(rg[u]);i++)
if(!vis[rg[u][i]])
dfs2(rg[u][i]);
}
int main()
{
int n,m;
si(n);si(m);
for(int i=0;i<m;i++)
{
int u,v;
si(u);si(v);
g[u].PB(v);
rg[v].PB(u);
}
for(int i=1;i<=n;i++)
if(!vis[i])
dfs1(i);
SET(vis,0);
for(int i=1;i<=n;i++)
{
int v = order[n-i];
if(!vis[v])
{
dfs2(v);
if(SZ(comp)>1)
for(auto &it:comp)
ans[it]=1;
comp.clear();
}
}
for(int i=1;i<=n;i++)printf("%d ",ans[i]);
printf("n");
return 0;
}

Second solution

#include<bits/stdc++.h>

const int N = 200050;

using namespace std;

int n, m;
vector<int> g[N], gr[N];
int used[N];
vector<int> order;
vector<int> comp;
int ans[N];

void dfs(int v)
{
used[v] = 1;
for (int i = 0; i < g[v].size(); i++)
{
int to = g[v][i];
if (used[to])
continue;
dfs(to);
}
order.push_back(v);
}

void dfs2(int v)
{
comp.push_back(v);
used[v] = 1;
for (int i = 0; i < gr[v].size(); i++)
{
int to = gr[v][i];
if (used[to])
continue;
dfs2(to);
}
}

int main(){
ios_base::sync_with_stdio(0);
//cin.tie(0);

cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int a, b;
cin >> a >> b;
g[a].push_back(b);
gr[b].push_back(a);
}

for (int i = 1; i <= n; i++)
{
if (used[i])
continue;
dfs(i);
}

reverse(order.begin(), order.end());

for (int i = 1; i <= n; i++)
{
used[i] = 0;
}

for (int i = 0; i < order.size(); i++)
{
int id = order[i];
if (used[id])
continue;
comp.clear();
dfs2(id);
if (comp.size() == 1)
continue;
for (int j = 0; j < comp.size(); j++)
{
ans[comp[j]] = 1;
}
}

for (int i = 1; i <= n; i++)
{
if (i>1)
cout << " ";
cout << ans[i];
}
cout << endl;

return 0;
}
coding problems solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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