Problem:
Topological Sort
A directed acyclic graph (DAG) can be used to represent the ordering of tasks. Tasks are represented by vertices and constraints where one task can begin before another, are represented by edges. For example, in the above example, you can undertake task B after both task A and task B are finished. You can obtain the proper sequence of all the tasks by a topological sort.
Given a DAG $G$, print the order of vertices after the topological sort.
Input
A directed graph $G$ is given in the following format:
$|V|\;|E|$
$s_0 \; t_0$
$s_1 \; t_1$
:
$s_{|E|-1} \; t_{|E|-1}$
$|V|$ is the number of vertices and $|E|$ is the number of edges in the graph. The graph vertices are named with the numbers $0, 1,..., |V|-1$ respectively.
$s_i$ and $t_i$ represent source and target nodes of $i$-th edge (directed).
Output
Print the vertices numbers in order. Print a number in a line.
If there are multiple possible solutions, print any one of them (the solution is judged by a special validator).
Constraints
$1 \leq |V| \leq 10,000$
$0 \leq |E| \leq 100,000$
There are no parallel edges in $G$
There are no self loops in $G$
Sample Input
6 6
0 1
1 2
3 1
3 4
4 5
5 2
Sample Output
0
3
1
4
5
2

Submitted solutions:

code_snippet: #include <iostream>
#include <vector>
#include <climits>
#include <queue>
#include <algorithm>
using namespace std;
const int MAXVEX = 10000;

// 需要有向无环图

class Graph {
public:
    int n; // 顶点数
    int m; // 边数
    vector<vector<int>> adjList; // 邻接表

    int visited[MAXVEX];
    int inDegree[MAXVEX]; // 各个顶点的入度

    Graph() {};
    Graph(int n, int m) : n(n), m(m) {
        adjList.resize(n);
        fill(inDegree, inDegree + n, 0);
    };

    void CreateList(); // 根据输入创建邻接表

    vector<int> TopologicalSort(); // 返回拓扑排序后的顶点
};

void Graph::CreateList() {
    for (int i = 0; i < m; i++) {
        int s, t;
        cin >> s >> t;
        adjList[s].push_back(t);
		inDegree[t]++;
    }
}

vector<int> Graph::TopologicalSort() {
    // 基于 BFS
    queue<int> q;

    // 将所有入度为 0 的节点入队
    for (int i = 0; i < n; i++) {
        if (inDegree[i] == 0) {
            q.push(i);
        }
    }

    vector<int> topoResult;

    while (!q.empty()) {
        int v = q.front();
        q.pop();
        topoResult.push_back(v);

        // 遍历 v 指向的所有节点
        for (int u: adjList[v]) {
            inDegree[u]--;
            if (inDegree[u] == 0) {
                q.push(u); // 将入度为 0 的节点入队
            }
        }
    }

    // 如果拓扑序节点数 != n，说明有环
    return topoResult;
}

int main() {
    int n, m;
    cin >> n >> m;
    Graph G(n, m);

    G.CreateList();
    vector<int> result = G.TopologicalSort();
    for (int x : result) {
        cout << x << endl;
    }
    
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3916
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    int N, M;
    cin >> N >> M;

    vector<int> in_deg(N, 0);
    vector<vector<int>> graph(N);
    for (int i = 0; i < M; i++) {
        int s, t; cin >> s >> t;
        graph[s].push_back(t);
        in_deg[t]++;
    }

    queue<int> que;
    for (int i = 0; i < N; i++) {
        if (in_deg[i] == 0) que.push(i);
    }

    vector<int> result;
    while (!que.empty()) {
        int now = que.front(); que.pop();
        result.push_back(now);
        for (auto nex : graph[now]) {
            in_deg[nex]--;
            if (in_deg[nex] == 0) que.push(nex);
        }
    }

    for (int i = 0; i < N; i++) {
        cout << result[i] << " ";
    }

    cout << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4036
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define rrep(i, a, n) for (int i = n - 1; i >= a; --i)
using ll = long long;
using ld = long double;
using pl = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vp = vector<pl>;
using vvp = vector<vp>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vs = vector<string>;
using vc = vector<char>;
using vvc = vector<vc>;
const int mod = 1000000007;
const ll hashh = 2147483647;

int main()
{
    ll v, e;
    cin >> v >> e;
    vl s(e), t(e), a(v);
    vvl g(v);
    rep(i, 0, e)
    {
        cin >> s[i] >> t[i];
        g[s[i]].push_back(t[i]);
        a[t[i]]++;
    }
    queue<ll> q;
    rep(i, 0, v)
    {
        if (a[i] == 0)
        {
            q.push(i);
        }
    }
    vl ans;
    while (!q.empty())
    {
        ll p = q.front();
        q.pop();
        for (auto &z : g[p])
        {
            a[z]--;
            if (a[z] == 0)
                q.push(z);
        }
        ans.push_back(p);
    }
    for (auto &z : ans)
    {
        cout << z << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 4884
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define YES cout<<"Yes"<<endl
#define NO cout<<"No"<<endl
using ll=long long;
using pi=pair<int,int>;
const int di[]={+1,-1,+0,+0};
const int dj[]={+0,+0,+1,-1};
const int INF=1e9;
//const ll INF=1e18;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<vector<int>> G(n);
    vector<int> deg(n, 0);
    rep(i, m) {
        int s, t;
        cin >> s >> t;
        G[s].push_back(t);
        deg[t]++;
    }
    vector<bool> seen(n, false);
    vector<int> topo;
    auto dfs=[&](auto dfs, int v) -> void {
        seen[v]=true;
        for(int nv:G[v]) if(!seen[nv]) {
            dfs(dfs, nv);
        }
        topo.push_back(v);
    };
    rep(i, n) if(!seen[i]) dfs(dfs, i);
    reverse(all(topo));
    for(int x:topo) cout << x << endl;

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4012
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <stack>

const int MAXN = 10000 + 1;

std::vector<int> G[MAXN];
bool visited[MAXN];
std::stack<int> si;

void dfs(int v)
{
    visited[v] = true; 
    for (int i = 0; i < G[v].size(); ++i) {
        if (!visited[G[v][i]]) {
            dfs(G[v][i]);
        }
    }
    si.push(v);
}

int main()
{
    int v, e;
    std::cin >> v >> e;
    for (int i = 0; i < e; ++i) {
        int s, t;
        std::cin >> s >> t;
        G[s].push_back(t);
    }

    for (int i = 0; i < v; ++i) {
        if (!visited[i]) {
            dfs(i);
        }
    }

    while (!si.empty()) {
        std::cout << si.top() << std::endl;
        si.pop();
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4012
final_rank: 7

code_snippet: using namespace std;
#include <bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define si set<int>
#define sll set<long long>
#define sc set<char>
#define ss set<string>
#define usi unordered_set<int>
#define qi queue<int>
#define pqi priority_queue<int>
#define pqll priority_queue<ll>
#define mii map<int, int>
#define umii unordered_map<int, int>
#define pii pair<int, int>
#define pllll pair<long long, long long>
#define f(i,s,e) for(int i = s; i < e; i++)
#define cf(i,s,e) for(int i = s; i <= e; i++)
#define rf(i,e,s) for(int i = e-1; i >= s; i--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define eb embrace_back
#define mp make_pair
#define fi first
#define se second
#define ub upper_bound
#define lb lower_bound
#define sz(x) (int)(x).size()
#define fastioa ios::sync_with_stdio(0); cin.tie(0); freopen("a.in", "r", stdin); freopen("a.out", "w", stdout);
#define fastio ios::sync_with_stdio(0); cin.tie(0);
#define mod 1000000007  //1e9 + 7
#define inf 1e9  // Use for int (e.g., distance)
#define inf_ll 1e18  // Use for long long
#define eps 1e-9  // Precision for floating-point comparisons

int main() {
	fastio
	int v, e; cin >> v >> e;
	vector<si> e_out(v);
	vi deg_in(v);

	f(i, 0, e) {
		int s, t; cin >> s >> t;
		e_out[s].insert(t);
		deg_in[t]++;
	}

	qi ans;
	f(i, 0, v) if (deg_in[i] == 0) ans.push(i);
	while (!ans.empty()) {
		auto x = ans.front(); ans.pop();
		cout << x << endl;
		for (auto y: e_out[x]) {
			deg_in[y]-- ;
			if (deg_in[y] == 0) ans.push(y);
		}
	}
}
accuracy: 1
time_ms: 10
memory_kb: 5576
final_rank: 18

code_snippet: // Lab1-2 Topological Sort.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<vector<int>> g;
vector<int> indeg;
vector<bool> visited;

vector<int> Kahn()
{
    visited.clear(); visited.resize(indeg.size());
    vector<int> ans;
    queue<int> q;
    for (int i = 0; i < indeg.size(); i++)
    {
        if (indeg[i] == 0) q.push(i);
    }
    while (q.size())
    {
        auto f = q.front(); q.pop();
        ans.push_back(f);
        for (auto n : g[f])
        {
            if (!visited[n])
            {
                indeg[n]--;
                if (indeg[n] == 0)
                {
                    visited[n] = true;
                    q.push(n);
                }
            }
        }
    }
    return ans;
}

int main()
{
    int v, e;
    cin >> v >> e;
    g.resize(v);
    indeg.resize(v);
    for (int i = 0; i < e; i++)
    {
        int f, t;
        cin >> f >> t;
        g[f].push_back(t);
        indeg[t]++;
    }

    auto ans = Kahn();
    for (auto i : ans)
    {
        cout << i << "\n";
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3896
final_rank: 4

code_snippet: #include <iostream>
#include <vector>
#include <queue>

using namespace std;

int main()
{
    int V, E; cin >> V >> E;

    vector<vector<int>> adj(V);
    vector<int> in_degree = vector(V, 0);

    for (int i = 0; i < E; i++)
    {
        int a, b; cin >> a >> b;
        adj[a].push_back(b);
    }

    for (auto list : adj)
    {
        for (auto node : list)
        {
            in_degree[node]++;
        }
    }

    queue<int> to_search;
    vector<int> topological;

    for (int i = 0; i < V; i++)
    {
        if (in_degree[i] == 0)
        {
            to_search.push(i);
        }
    }

    while (!to_search.empty())
    {
        int current = to_search.front(); to_search.pop();
        topological.push_back(current);

        for (auto node : adj[current])
        {
            in_degree[node]--;
            if (in_degree[node] == 0)
            {
                to_search.push(node);
            }
        }
    }

    for (auto i : topological)
    {
        cout << i << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3892
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define ll long long

const ll MAXN=1e4+5;

ll n, m, indeg[MAXN];
vector<ll> adj[MAXN];
queue<ll> indeg0;

int main()
{
    cin >> n >> m;
    for (int i=0; i<m; i++)
    {
        ll a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        indeg[b]++;
    }
    for (int i=0; i<n; i++)
    {
        if (indeg[i]==0)
        {
            indeg0.push(i);
        }
    }
    while (!indeg0.empty())
    {
        ll pos=indeg0.front();
        indeg0.pop();
        cout << pos << "\n";
        for (auto i:adj[pos])
        {
            indeg[i]--;
            if (indeg[i]==0)
            {
                indeg0.push(i);
            }
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 4060
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

vector<ll> topologicalsort(vector<vector<ll>> e){
    int n = e.size();
    vector<ll> ent(n),res;
    for(ll i = 0;i<n;i++) for(ll to : e[i])ent[to]++;
    queue<ll> q;
    vector<bool> seen(n);
    for(ll i = 0;i<n;i++) if(ent[i] == 0) {
        q.emplace(i);
        seen[i] = true;
        res.emplace_back(i);
    }
    while(q.size()){
        ll cur = q.front(); q.pop();
        for(ll to : e[cur]){
            ent[to]--;
            if(ent[to] == 0){
                q.emplace(to);
                seen[to] = true;
                res.emplace_back(to);
            }
        }
    }
    return res;
};
int main(){
    ll n,m; cin >> n >> m;
    vector e(n,vector<ll>());
    while(m--){
        ll a,b; cin >> a >> b;
        e[a].emplace_back(b);
    }
    auto res = topologicalsort(e);
    for(ll i : res)cout << i << '\n';
}
accuracy: 1
time_ms: 10
memory_kb: 4716
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;

const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

const int MAX = 10000;

int color[MAX];
vector<int> g[MAX];

list<int> out;

int V, E;

void dfs(int u)
{
    color[u] = GRAY;

    for (auto v : g[u])
    {
        if (color[v] == WHITE)
            dfs(v);
    }

    color[u] = BLACK;
    out.push_front(u);
}

void topologucalSort()
{
    for (int i = 0; i < V; i++)
    {
        color[i] = WHITE;
    }

    for (int i = 0; i < V; i++)
    {
        if (color[i] == WHITE) dfs(i);
    }
}

int main()
{
    cin >> V >> E;
    for (int i = 0; i < E; i++)
    {
        int s, t;
        cin >> s >> t;

        g[s].push_back(t);
    }

    topologucalSort();

    for (auto v : out)
    {
        cout << v << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4108
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (n); i++)


// vectorを出力する
void vecprint(vector<ll> vec) {
    rep(i, vec.size()) {
        cout << vec[i] << endl;
    }
}
void newvecprint(vector<ll> vec) {
    rep(i, vec.size()) {
        cout << vec[i] << " ";
    }
    cout << endl;
}

// 最大か最小を求める
ll minval(vector<ll> vec) {
    return *min_element(begin(vec), end(vec));
}
ll maxval(vector<ll> vec) {
    return *max_element(begin(vec), end(vec));
}

vector<string> color;
list<ll> ans;


void dfs(ll &u, vector<vector<ll>> &G) {
    color[u] = "gray";
    for (auto v : G[u]) {
        if (color[v] == "white") {
            dfs(v, G);
        }
    }
    ans.push_front(u);
}

void topologicalSort(vector<vector<ll>> G, ll v) {
    color.resize(v);
    rep(i,v) {
        color[i] = "white";
    }
    rep(i,v) {
        if (color[i] == "white") {
            dfs(i, G);
        }
    }
}



void solve() {
    ll v, e;
    cin >> v >> e;
    vector<vector<ll>> G(v);
    rep(i, e) {
        ll s, t;
        cin >> s >> t;
        G[s].push_back(t);
    }
    topologicalSort(G, v);
    for (list<ll>::iterator it = ans.begin(); it != ans.end(); it++) {
        cout << *it << endl;
    }

    return;
}

int main() {
    solve();
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 5504
final_rank: 17

code_snippet: #include <bits/stdc++.h>

using namespace std;

using ll = long long;
using Graph = vector<vector<int>>;
#define all(v) v.begin(), v.end()

#define debug(x) cout << #x << " : " << x << endl;
#define debug2(x, y) cout << #x << " : " << x << " , " << #y << " : " << y << endl;
#define debugv(v) cout << #v << " : "; for(auto x: v) cout << x << ", "; cout << endl;
#define debugvv(vv) cout << #vv << " :" << endl; for(auto v: vv){for(auto x: v){cout << x << ", ";} cout << endl;}
#define debugvvp(vvp) cout << #vvp << " :" << endl; for(auto vp: vvp){ \
                        for(auto p: vp){cout << "(" << p.first << ", " << p.second << "), ";} cout << endl;}
#define debugcv(v, n) cout << #v << " : "; for(int i=0;i<n;i++) cout << v[i] << ", "; cout << endl;
#define debugcvv(vv, m, n) cout << #vv << " :" << endl; for(int i=0;i<m;i++){ \
                            for(int j=0;j<n;j++){cout << vv[i][j] << ", ";} cout << endl;}


void dfs(const Graph &G, int u, vector<bool> &seen, vector<int> &order){
    seen[u] = true;
    for(auto v : G[u]) if(!seen[v]) dfs(G, v, seen, order);
    order.push_back(u);
}

int main(){
    int n, m, i; cin >> n >> m;
    vector<bool> seen(n);
    vector<int> order;
    Graph G(n);

    for(i = 0; i < m; i++){
        int s, t; cin >> s >> t;
        G[s].push_back(t);
    }

    for(i = 0; i < n; i++) if(!seen[i]) dfs(G, i, seen, order);

    reverse(all(order));
    for(i = 0; i < n; i++) cout << order[i] << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3756
final_rank: 1

code_snippet: #include <bits/stdc++.h>

using namespace std;

using ll = long long;
#define all(v) v.begin(), v.end()

#define debug(x) cout << #x << " : " << x << endl;
#define debug2(x, y) cout << #x << " : " << x << " , " << #y << " : " << y << endl;
#define debugv(v) cout << #v << " : "; for(auto x: v) cout << x << ", "; cout << endl;
#define debugvv(vv) cout << #vv << " :" << endl; for(auto v: vv){for(auto x: v){cout << x << ", ";} cout << endl;}
#define debugvvp(vvp) cout << #vvp << " :" << endl; for(auto vp: vvp){ \
                        for(auto p: vp){cout << "(" << p.first << ", " << p.second << "), ";} cout << endl;}
#define debugcv(v, n) cout << #v << " : "; for(int i=0;i<n;i++) cout << v[i] << ", "; cout << endl;
#define debugcvv(vv, m, n) cout << #vv << " :" << endl; for(int i=0;i<m;i++){ \
                            for(int j=0;j<n;j++){cout << vv[i][j] << ", ";} cout << endl;}


int main(){
    int n, m, i; cin >> n >> m;
    vector<int> ind(n);
    vector<vector<int>> G(n);
    queue<int> que;

    for(i = 0; i < m; i++){
        int s, t; cin >> s >> t;
        G[s].push_back(t);
        ind[t]++;
    }

    for(i = 0; i < n; i++) if(ind[i] == 0) que.push(i);

    while(!que.empty()){
        int u = que.front();
        que.pop();
        cout << u << endl;
        for(auto v : G[u]) if(--ind[v] == 0) que.push(v);
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3864
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i<n; i++)

static const int MAX = 100000;

vector<int> G[MAX];
list<int> out;
bool V[MAX];
int N;
int indeg[MAX];

void bfs(int s) {
    queue<int> q;
    q.push(s);
    V[s] = true;

    while (!q.empty()) {
        int u = q.front(); q.pop();
        out.push_back(u);
        for (int i = 0; i < G[u].size(); i++) {
            int v = G[u][i];
            indeg[v]--;
            if (indeg[v] == 0 && !V[v]) {
                V[v] = true;
                q.push(v);
            }
        }
    }
}

void tsort() {
    for (int i=0; i<N; i++) {
        indeg[i] = 0;
    }

    for (int u=0; u<N; u++) {
        for (int i=0; i<G[u].size(); i++) {
            int v = G[u][i];
            indeg[v]++;
        }
    }

    for (int u=0; u<N; u++) {
        if (indeg[u] == 0 && !V[u]) bfs(u);
    }

    for (list<int>::iterator it = out.begin(); it != out.end(); it++) {
        cout << *it << endl;
    }
}

int main()
{
    int s, t, M;
    cin >> N >> M;

    for (int i=0; i<N; i++) V[i] = false;

    for (int i=0; i<M; i++) {
        cin >> s >> t;
        G[s].push_back(t);
    }

    tsort();

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 6532
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;

const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

const int MAX = 10000;

int color[MAX];
vector<int> g[MAX];

list<int> out;

int V, E;

void dfs(int u)
{
    color[u] = GRAY;

    for (auto v : g[u])
    {
        if (color[v] == WHITE)
            dfs(v);
    }

    color[u] = BLACK;
    out.push_front(u);
}

void topologucalSort()
{
    for (int i = 0; i < V; i++)
    {
        color[i] = WHITE;
    }

    for (int i = 0; i < V; i++)
    {
        if (color[i] == WHITE) dfs(i);
    }
}

int main()
{
    cin >> V >> E;
    for (int i = 0; i < E; i++)
    {
        int s, t;
        cin >> s >> t;

        g[s].push_back(t);
    }

    topologucalSort();

    for (auto v : out)
    {
        cout << v << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4304
final_rank: 14

code_snippet: // https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/4/GRL_4_B
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;

using Graph = vector<vector<size_t>>;

void dfs(const Graph &G, size_t v, vector<bool> &used, list<size_t> &ans)
{
    used[v] = true; // 探索済み
    for (size_t t : G[v])
    {
        if (!used[t]) // tが未探索なら
        {
            dfs(G, t, used, ans);
        }
    }
    ans.push_front(v); // 帰りがけにpush
}

list<size_t> topological_sort(const Graph &G)
{
    list<size_t> ans;
    size_t n = G.size();
    vector<bool> used(n, false); // 探索済みか否か
    for (size_t i = 0; i < n; i++)
    {
        // 未探索の頂点ごとにDFS
        if (!used[i])
            dfs(G, i, used, ans);
    }
    return ans;
}

int main()
{
    size_t num_vertices; // 頂点数
    size_t num_edges;    // 辺数
    cin >> num_vertices >> num_edges;
    Graph G(num_vertices); // グラフ

    for (size_t i = 0; i < num_edges; i++)
    {
        size_t s, t;
        cin >> s >> t;
        G[s].push_back(t);
    }

    // 処理順
    list<size_t> order = topological_sort(G);

    // 結果を出力
    for (size_t node : order)
    {
        cout << node << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4188
final_rank: 13

code_snippet: // https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/4/GRL_4_B
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

using Graph = vector<vector<size_t>>;

void dfs(const Graph &G, int v, vector<bool> &used, vector<size_t> &ans)
{
    used[v] = true; // 探索済み
    for (size_t t : G[v])
    {
        if (!used[t]) // tが未探索なら
        {
            dfs(G, t, used, ans);
        }
    }
    ans.push_back(v); // 帰りがけにpush_back
}

vector<size_t> topological_sort(const Graph &G)
{
    vector<size_t> ans;
    size_t n = G.size();
    vector<bool> used(n, false); // 探索済みか否か
    for (int i = 0; i < n; i++)
    {
        // 未探索の頂点ごとにDFS
        if (!used[i])
            dfs(G, i, used, ans);
    }
    reverse(ans.begin(), ans.end()); // 逆順に
    return ans;
}

int main()
{
    size_t num_vertices; // 頂点数
    size_t num_edges;    // 辺数
    cin >> num_vertices >> num_edges;
    Graph G(num_vertices); // グラフ

    for (size_t i = 0; i < num_edges; i++)
    {
        size_t s, t;
        cin >> s >> t;
        G[s].push_back(t);
    }

    // 処理順
    vector<size_t> order = topological_sort(G);

    // 結果を出力
    for (size_t node : order)
    {
        cout << node << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3888
final_rank: 6

code_snippet: // https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/4/GRL_4_B
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

using Graph = vector<vector<size_t>>;

void dfs(const Graph &G, int v, vector<bool> &used, vector<size_t> &ans)
{
    used[v] = true; // 探索済み
    for (size_t t : G[v])
    {
        if (!used[t]) // tが未探索なら
        {
            dfs(G, t, used, ans);
        }
    }
    ans.push_back(v); // 帰りがけにpush_back
}

vector<size_t> topological_sort(const Graph &G)
{
    vector<size_t> ans;
    size_t n = G.size();
    vector<bool> used(n, false); // 探索済みか否か
    for (int i = 0; i < n; i++)
    {
        // 未探索の頂点ごとにDFS
        if (!used[i])
            dfs(G, i, used, ans);
    }
    reverse(ans.begin(), ans.end()); // 逆向きなのでひっくり返す
    return ans;
}

int main()
{
    size_t num_vertices; // 頂点数
    size_t num_edges;    // 辺数
    cin >> num_vertices >> num_edges;
    Graph G(num_vertices); // グラフ

    for (size_t i = 0; i < num_edges; i++)
    {
        size_t s, t;
        cin >> s >> t;
        G[s].push_back(t);
    }

    // 処理順
    vector<size_t> order = topological_sort(G);

    // 結果を出力
    for (size_t node : order)
    {
        cout << node << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4156
final_rank: 12

code_snippet: // https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/4/GRL_4_B
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Edge
{
    size_t s, t;
};

// この頂点を処理する準備が完了しているかを判定
bool isAvailable(size_t vertex, const vector<bool> &is_finished, const vector<Edge> &edge)
{
    for (const Edge &e : edge)
    {
        if (e.t == vertex)
        {
            if (!is_finished[e.s]) // e.sが未処理
            {
                return false; // この頂点はまだ処理できない
            }
        }
    }

    return true;
}

int main()
{
    size_t num_vertices; // 頂点数
    size_t num_edges;    // 辺数
    cin >> num_vertices >> num_edges;
    vector<size_t> order;         // 処理順
    vector<Edge> edge(num_edges); // 辺配列

    vector<bool> is_finished(num_vertices, true); // 完了済みか否か

    for (Edge &e : edge)
    {
        cin >> e.s >> e.t;
        is_finished[e.t] = false; // 未処理にする
    }

    for (size_t i = 0; i < num_vertices; i++)
    {
        if (is_finished[i])
            order.push_back(i); // 無条件で処理可能な頂点を追加
    }

    while (order.size() < num_vertices) // orderに全頂点が追加されるまで繰り返す
    {
        for (Edge &e : edge)
        {
            if (is_finished[e.t])
                continue;

            if (isAvailable(e.t, is_finished, edge)) // e.tが処理可能なら
            {
                order.push_back(e.t); // orderに追加
                is_finished[e.t] = true;
            }
        }
    }

    // 結果を出力
    for (size_t node : order)
    {
        cout << node << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 3944
final_rank: 20