Cod sursa(job #1236604)

Utilizator ArmandNMArmand Nicolicioiu ArmandNM Data 2 octombrie 2014 10:59:18
Problema Componente tare conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.66 kb
#include <fstream>
#include <vector>

const int NMAX = 305;

using namespace std;
ifstream f("examene.in");
ofstream g("examene.out");

int N,M,P,x,y;
vector <int> v[NMAX],vt[NMAX];
bool used[NMAX],in_cicle[NMAX],important[NMAX],pos[NMAX][NMAX];

void DFS(int nod)
{
    used[nod] = true;
    for (int i = 0; i < v[nod].size(); ++i)
    {
        int vecin = v[nod][i];
        pos[nod][vecin] = true;
        if (pos[vecin][nod])
        {
            in_cicle[vecin] = true;
            in_cicle[nod] = true;
        }
        if (!used[vecin])
            DFS(vecin);
        else in_cicle[nod];
    }
}

void DFSt(int nod)
{
    used[nod] = true;
    important[nod] = true;
    for (int i = 0; i < vt[nod].size(); ++i)
    {
        int vecin = vt[nod][i];
        if (!used[vecin])
        {
            DFSt(vecin);
        }
    }
}

int main()
{
    f >> N >> M >> P;
    for (int i = 1; i <= M; ++i)
    {
        f >> x >> y;
        v[x].push_back(y);
        vt[y].push_back(x);
    }
    for (int i = 1; i <= P; ++i)
    {
        f >> x;
        important[x] = true;
    }

    for (int i = 1; i <= N; ++i)
    {
        if (important[i])
            DFSt(i);
    }

    for (int i = 1; i <= N; ++i)
    {
        if (!important[i])
            g << i << " ";
    }

    for (int i = 1; i <= N; ++i)
        used[i] = false;

    for (int i = 1; i <= N; ++i)
    {
        if (!used[i])
        {
            DFS(i);
        }
    }

    g << '\n';

    for (int i = 1; i <= N; ++i)
    {
        if (in_cicle[i])
            g << i << " ";
    }

    f.close();
    g.close();
    return 0;
}