Mai intai trebuie sa te autentifici.
Cod sursa(job #2084270)
| Utilizator | Data | 8 decembrie 2017 21:18:50 | |
|---|---|---|---|
| Problema | Sortare topologica | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.66 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream f("sortaret.in");
ofstream g("sortaret.out");
const int nmax = 50005;
vector<int> ls[nmax];
int viz[nmax], x, y, n, m, stiva[nmax], vf, i;
void dfs(int x) {
int l = ls[x].size(), i, y;
viz[x] = 1;
for (i = 0; i < l; i++) {
y = ls[x][i];
if (viz[y]) continue;
dfs(y);
}
stiva[++vf] = x;
}
int main() {
f >> n >> m;
while (m--) {
f >> x >> y;
ls[x].push_back(y);
ls[y].push_back(x);
}
for (i = 1; i <= n; i++)
if (viz[i] == 0)
dfs(i);
while (vf)
g<<stiva[vf--]<<' ';
}
