Topological sort using STL
Headers and namespaces

#include "bits/stdc++.h"


That's not a standard C++ header; prefer to use standard headers so that others can use your code.

using namespace std;


Bringing all names in from a namespace is problematic; namespace std particularly so. See Why is “using namespace std” considered bad practice?.

Used but not included: &lt;cstdio&gt;, &lt;vector&gt;,  &lt;list&gt;.  I'd recommend you stick to one of &lt;cstdio&gt; and &lt;iostream&gt;.

Function signature

std::vector&lt;int&gt; topologicalsort(std::vector&lt;std::list&lt;int&gt; &gt; adjacencylist,
                                 int indegree[], int v,
                                 int e)


Why do you require e?  It's never used.

Passing indegree as a pointer and length is very C-style; prefer a C++ range (e.g. a pair of iterators, or a standard container).

There's no need to pass adjacencylist by value: a const-ref is more appropriate.

Result vector

Instead of creating v+1 elements in the result vector and having to keep track of our insert position with k, we can create it empty and push_back each result as we find it.  We can avoid reallocation by using reserve().

Looping over elements

Instead of the error prone iterator-based loop, prefer to use range-based for when applicable:

    for (int i: adjacencylist[x]) {
        if (--indegree[i] == 0)
            indegree0.push_back(i);
    }


Variable-length arrays

This is a non-standard extension:

int indegree[v+1];


It's not clear why you ignore the 0th element of these arrays; that probably warrants a comment.



Cleaned code

#include &lt;iostream&gt;
#include &lt;list&gt;
#include &lt;vector&gt;

std::vector&lt;int&gt; topologicalsort(const std::vector&lt;std::list&lt;int&gt;&gt;&amp; adjacencylist,
                                 std::vector&lt;int&gt; indegree)
{
    std::vector&lt;int&gt; ans;
    ans.reserve(indegree.size()+1);
    //list to store vertices with indegree 0
    std::list&lt;int&gt; indegree0;
    for (size_t i = 1;  i &lt; indegree.size();  i++) {
        if (indegree[i] == 0)
            indegree0.push_back(i);
    }
    while (!indegree0.empty()) {
        const int x = indegree0.front();
        indegree0.pop_front();
        ans.push_back(x);
        for (const int i: adjacencylist[x]) {
            if (--indegree[i] == 0)
                indegree0.push_back(i);
        }
    }
    return ans;
}

int main()
{
    int v, e;
    std::cin &gt;&gt; v &gt;&gt; e;
    std::vector&lt;std::list&lt;int&gt;&gt; adjacencylist(v+1);
    std::vector&lt;int&gt; indegree(v+1);
    for (int i = 1;  i &lt;= e;  i++) {
        int v1, v2;
        std::cin &gt;&gt; v1 &gt;&gt; v2;
        adjacencylist[v1].push_back(v2);
        ++indegree[v2];
    }
    for (int i = 1;  i &lt;= v;  i++) {
        std::cout &lt;&lt; "adjacencylist[" &lt;&lt; i &lt;&lt; "%d] :";
        for (int j: adjacencylist[i]) {
            std::cout &lt;&lt; j &lt;&lt; "  ";
        }
        std::cout &lt;&lt; std::endl;
    }
    auto const&amp; ans = topologicalsort(adjacencylist, std::move(indegree));
    for (int i: ans)
        std::cout &lt;&lt; i &lt;&lt; "  ";
}