Cod sursa(job #2405508)

Utilizator eduardcadarCadar Eduard eduardcadar Data 14 aprilie 2019 16:25:02
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream f("sortaret.in");
ofstream g("sortaret.out");
#define dim 8192
int pz;
char ax[dim];
void cit(int &x) {
    x = 0;
    while (ax[pz] < '0' || ax[pz] > '9')
        if (++pz == dim) f.read(ax,dim), pz = 0;
    while (ax[pz] >= '0' && ax[pz] <= '9') {
        x = x * 10 + ax[pz] - '0';
        if (++pz == dim) f.read(ax,dim), pz = 0;
    }
}
int n, m, t[50001], a[100001], b[100001], x, y;
bool u[50001];
int find(int i) {
    if (t[i] != i) t[i] = find(t[i]);
    return t[i];
}
void uneste(int i, int j) {
    i = find(i);
    j = find(j);
    if (i == j) return;
    t[i] = j;
}
vector<int> v[50001];
void afis(int nod) {
    if (u[nod]) return;
    for (int i = 0; i < v[nod].size(); ++i) afis(v[nod][i]);
    g << nod << ' ';
    u[nod] = 1;
}
int main()
{
    cit(n), cit(m);
    for (int i = 1; i <= m; ++i) {
        cit(x), cit(y);
        if (x-y) v[y].push_back(x);
    }
    /*for (int i = 1; i <= n; ++i) t[i] = i;
    for (int i = 1; i <= m; ++i) {
        if (a[i] > t[b[i]]) t[b[i]] = find(a[i]);
    }*/
    for (int i = 1; i <= n; ++i) afis(i);
    return 0;
}