Pagini recente » Cod sursa (job #1524646) | Cod sursa (job #2201393) | Cod sursa (job #1578495) | Cod sursa (job #192450) | Cod sursa (job #2414853)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("sortaret.in");
ofstream g("sortaret.out");
int main() {
int n, m;
f >> n >> m;
vector < vector < int > > G(n+1);
vector < int > gradExt(n + 1);
vector < vector < int > > tati(n+1);
vector < bool > afisat(n + 1, 0);
queue < int > Q;
for (int i = 0; i < m; i++) {
int x, y;
f >> x >> y;
G[x].push_back(y);
gradExt[x]++;
tati[y].push_back(x);
}
for (int i = 1; i <= n; i++){
if (gradExt[i] == 0) {
Q.push(i);
g << i << ' ';
}
}
while (!Q.empty()) {
int elem = Q.front();
Q.pop();
for (auto &i : tati[elem]) {
gradExt[i]--;
if (gradExt[i] == 0) {
Q.push(i);
g << i << ' ';
}
}
}
return 0;
}