Cod sursa(job #2565775)

Utilizator Andr31R0BAndreiRob Andr31R0B Data 2 martie 2020 16:42:22
Problema Componente tare conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream in("date.in");
ofstream out("date.out");

int a[101][101], n, v1[101], v2[101], c[101], nrc;

void df1(int x)
{
    v1[x]=1;
    for(int i=1; i<=n; i++)
        if(v1[i]==0 && a[x][i]==1)
            df1(i);
}
void df2(int x)
{
    v2[x]=1;
    for(int i=1; i<=n; i++)
        if(v2[i]==0 && a[i][x]==1)
            df2(i);
}

int main()
{
    in>>n;

    int x,y;

    while(in>>x>>y)
    {
        a[x][y]=1;
    }

    for(int i=1; i<=n; i++)
    {
        if(c[i]==0)
        {
            for(int j=1; j<=n; j++)
            {
                v1[j]=v2[j]=0;
            }
            nrc++;
            df1(i);
            df2(i);
            for(int j=1; j<=n; j++)
                if(v1[j]==1 && v2[j]==1)
                    c[j]=nrc;
        }
    }
    int k=1;
    while(k<=nrc)
    {
        for(int i=1; i<=n; i++)
        {
            if(c[i]==k)
                cout<<i<<' ';
        }
        k++;
        cout<<'\n';
    }

    in.close();
    return 0;
}