Pagini recente » Cod sursa (job #1943306) | Cod sursa (job #2294147) | Cod sursa (job #2956034) | Cod sursa (job #2472325) | Cod sursa (job #3199376)
#include <bits/stdc++.h>
using dt = ushort;
struct node : std::vector<dt> {
int in_deg = 0;
};
void toposort(std::vector<node>& vec) {
std::queue<dt> free;
for(dt i = 1; i < vec.size(); i++)
if(vec[i].in_deg == 0) free.emplace(i);
while(!free.empty()) {
auto& nod = vec[free.front()];
std::cout << free.front() << ' ';
free.pop();
//std::random_shuffle(nod.begin(), nod.end());
for(auto v : nod) {
if(--vec[v].in_deg == 0)
free.push(v);
}
}
}
int main() {
freopen("sortare.in", "r", stdin);
freopen("sortare.out", "w", stdout);
int n, m; std::cin >> n >> m;
std::vector<node> noduri(n + 1);
while(m--) {
dt a, b; std::cin >> a >> b;
noduri[a].emplace_back(b);
noduri[b].in_deg++;
}
noduri[0].in_deg = -1;
toposort(noduri);
return 0;
}