Mai intai trebuie sa te autentifici.
Cod sursa(job #2501790)
| Utilizator | Data | 30 noiembrie 2019 11:06:32 | |
|---|---|---|---|
| Problema | Sortare topologica | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.78 kb |
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
vector <int> graph[50005];
queue <int> q;
int n, m, x, y, gr[50005];
void lee()
{
while(!q.empty())
{
int nod=q.front();
q.pop();
for(auto &v:graph[nod])
{
gr[v]--;
if(gr[v] == 0)
q.push(v);
}
printf("%d ", nod);
}
}
int main()
{
freopen("sortaret.in", "r", stdin);
freopen("sortaret.out", "w", stdout);
scanf("%d %d", &n, &m);
for(int i=1; i<=m; i++)
{
scanf("%d %d", &x, &y);
graph[x].push_back(y);
gr[y]++;
}
for(int i=1; i<=n; i++)
if(gr[i]==0)
q.push(i);
lee();
return 0;
}
