Please share the approach how to do it.
Heres my code , Its failing the second test case.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n;
bool visited[100000]={0};
vector v;
ll x,y;
map<ll,ll> mp,val;
void dfs(ll s){
v.push_back(s);
visited[s]=1;
if(mp[s])
dfs(mp[s]);
}
int main()
{
cin>>n;
ll e;
ll s=n+1;
vector<ll> a;
for(ll i=1;i<=n;i++){
cin>>e;
if(e==-1){
a.push_back(i);
}
else
mp[e]=i;
}
x=1,y=n;
for(ll i=0;i<a.size();i++){
if(!visited[a[i]]){
v.clear();
dfs(a[i]);
for(ll i=v.size()-1;i>0;i--){
val[v[i]]=x++;
}
val[v[0]]= y--;
}
}
for(auto it=val.begin();it!=val.end();it++){
cout<<it->second<<" ";
}
return 0;
}