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("%d\n",n)
#define sll(n) scanf("%lld",&n)
#define lldout(n) printf("%lld\n",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;
}