Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : Minimal Labels

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

class Graph {
  public:
    int n;
    vector<vector<int>> adj;
    vector<int> indegree;

    Graph(int n) {
        this->n = n;
        adj.resize(n);
        indegree.resize(n);
    }

    void modified_topological_sort() {
        // Populate indegrees.
        for (int u = 0; u < n; u++) {
            for (auto child : adj[u]) {
                indegree[child]++;
            }
        }

        set<pair<int, int>> current_degrees;
        for (int u = 0; u < n; u++) {
            current_degrees.insert({indegree[u], u});
        }

        vector<int> res(n);
        for (int i = 0; i < n; i++) {
            int u = current_degrees.begin()->second;
            current_degrees.erase(current_degrees.begin());
            res[u] = i + 1;

            for (auto child : adj[u]) {
                if (indegree[child] > 0) {
                    current_degrees.erase({indegree[child], child});
                    indegree[child]--;
                    current_degrees.insert({indegree[child], child});
                }
            }
        }

        for (int i = 0; i < n; i++) {
            cout << res[i] << " ";
        }
        cout << "\n";
    }
};

void solve() {
    int n, m;
    cin >> n >> m;

    Graph g(n);
    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        g.adj[x].push_back(y);
    }

    g.modified_topological_sort();
}

int main() {
    solve();
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

class Graph {
  public:
    int n;
    // tadj is the adjacency list of the transpose graph.
    vector<vector<int>> adj, tadj;
    vector<int> outdegree;

    Graph(int n) {
        this->n = n;
        adj.resize(n);
        tadj.resize(n);
        outdegree.resize(n);
    }

    void modified_topological_sort() {
        // Populate outdegrees.
        for (int u = 0; u < n; u++) {
            for (auto child : adj[u]) {
                outdegree[u]++;
            }
        }

        // Sort (outdegree, index)
        // We want to always process process nodes with zero outdegree.
        // For all such nodes with zero outdegree, we will first remove the
        // vertex with the highest index.
        //
        // (1, 3) < (1, 2) < (2, 4)
        auto func = [](pair<int, int> small, pair<int, int> big) {
            if (small.first == big.first) {
                return small.second > big.second;
            }
            return small.first < big.first;
        };
        set<pair<int, int>, decltype(func)> current_degrees(func);

        for (int u = 0; u < n; u++) {
            current_degrees.insert({outdegree[u], u});
        }

        vector<int> res(n);
        for (int i = n - 1; i >= 0; i--) {
            int u = current_degrees.begin()->second;
            current_degrees.erase(current_degrees.begin());
            res[u] = i + 1;

            for (auto child : tadj[u]) {
                if (outdegree[child] > 0) {
                    current_degrees.erase({outdegree[child], child});
                    outdegree[child]--;
                    current_degrees.insert({outdegree[child], child});
                }
            }
        }

        for (int i = 0; i < n; i++) {
            cout << res[i] << " ";
        }
        cout << "\n";
    }
};

void solve() {
    int n, m;
    cin >> n >> m;

    Graph g(n);
    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        g.adj[x].push_back(y);
        g.tadj[y].push_back(x);
    }

    g.modified_topological_sort();
}

int main() {
    solve();
    return 0;
}