Cod sursa(job #2475523)

Utilizator XsoundCristian-Ioan Roman Xsound Data 17 octombrie 2019 01:01:58
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <bits/stdc++.h>
#define Nmax 50005
using namespace std;

ifstream fin("sortaret.in");
ofstream fout("sortaret.out");

vector < int > v[Nmax];

queue <int> q;

bitset < Nmax >b;

void BFS ( int x )
{

    fout << x << '\n' ;

    b[x] = 1;

    int lng = v[x].size();

    for ( int i = 0; i < lng; i++ )
        q.push(v[x][i]);

    while ( ! q.empty() )
    {
        x = q.front () ;
        b[x] = 1;
        q.pop();

        fout << x << '\n';

        lng = v[x].size();

        for ( int i = 0 ; i < lng; i++ )
          if( b[v[x][i]] == 0 )  q.push(v[x][i]);

    }

}

int main()
{
    int n, m,x ,y ;

    fin >> n >> m;

    for ( int i = 1; i <= m; i++ )
    {
        fin >> x >> y;

        v[x].push_back(y);
        v[y].push_back(x);
    }

    BFS( 1 );
}