Cod sursa(job #2230813)

Utilizator DandeacDan Deac Dandeac Data 11 august 2018 16:54:02
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.59 kb
#include <iostream>
#include <queue>
#include <fstream>
#include <cstring>
#include <vector>
using namespace std;

ifstream f ("sortaret.in");
ofstream g ("sortaret.out");

queue <int> q;
bool verif[100000];
//int a[10000];
vector <int> b;

int main()
{

    int n,m;
    f>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int x,y;
        f>>x>>y;
        if(verif[x] && !verif[y])
        {
            b.push_back(y);
        }else
        if(verif[x] && verif[y])
        {
            int poz=-1,I=0;
            bool scoate = false;
            for(auto It=b.begin(); It!=b.end(); ++It)
            {
                I++;
                if(*It == y) poz = I;
                if(*It == x && poz<I && poz!=-1)
                {
                    scoate = true;
                    break;
                }

            }
            if(scoate)
            {
                b.erase(b.begin() + poz-1);
                b.push_back(y);
            }
        }else
        if(!verif[x] && verif[y])
        {
            int I=0;

            for(auto It= b.begin();It!=b.end();++It)
            {
                I++;
                if(*It == y) break;

            }

                b.erase(b.begin() + I-1);
                b.push_back(x);
                b.push_back(y);

        }else
        if(!verif[x] && !verif[y])
        {
            b.push_back(x);
            b.push_back(y);
        }

        verif[x] = 1;
        verif[y] = 1;

    }
    for(auto It= b.begin();It!=b.end();++It)
    {
        g<<*It<<' ';
    }
    return 0;
}