Untitled
unknown
plain_text
a year ago
441 B
11
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());
}Editor is loading...
Leave a Comment