Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
441 B
4
Indexable
vector<int> adj[MAX]; // Danh sách kề
vector<bool> visited(MAX, false);
vector<int> topo;

void dfs(int v) {
    visited[v] = true;
    for (int u : adj[v]) {
        if (!visited[u]) {
            dfs(u);
        }
    }
    topo.push_back(v);
}

void topological_sort(int n) {
    for (int i = 0; i < n; i++) {
        if (!visited[i]) {
            dfs(i);
        }
    }
    reverse(topo.begin(), topo.end()); 
}
Leave a Comment