my code is failing on test case 1 tell we where i am going wrong?
here is my code—
#include <bits/stdc++.h>
using namespace std;
int n,ans[500001],arr[500001];
int indegree[500001]={-1};
int visited[500001];
int put;
int last;
void addEdge(vector adj[], int u, int v)
{
adj[u].push_back(v);
}
void topologicalsort(vector v[])
{
priority_queueq;
for(int i=1;i<=n;i++)
{
if(indegree[i]==0) {
q.push(i);
visited[i]=true; }
}
while(!q.empty())
{
int i = q.top();
q.pop();
for(int j=0;j<v[i].size();j++)
{
if(!visited[v[i][j]])
{
indegree[v[i][j]] --;
if(indegree[v[i][j]]==0)
{
ans[v[i][j]]=last;
last--;
q.push(v[i][j]);
visited[v[i][j]] = true;
}
}
}
}
}
int main() {
cin>>n;
for(int i=1;i<=n;i++) cin>>arr[i];
vector v[n+1];
int rev=n;
put=n;
indegree[500001]={0};
for(int i=1;i<=n;i++)
{
visited[i]=0;
if(arr[i]==-1)
{
ans[i] = rev;
rev–;
last=rev;
indegree[i]=0;
}
else{
addEdge(v,arr[i],i);
indegree[i]++;
}
}
topologicalsort(v);
for(int i=1;i<=n;i++)
cout<<ans[i]<<" ";
}