Cod sursa(job #1611874)

Utilizator ZenusTudor Costin Razvan Zenus Data 24 februarie 2016 15:33:48
Problema Componente tare conexe Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 kb
#include <bits/stdc++.h>

using namespace std;

const int nmax = 100009;

int r , mxr , cr , p , n , m , i , x , y;
stack < int > is;
vector < int > g[nmax] , t[nmax];
int mark[nmax];

void df(int x)
{
    for (int i = 0 ; i < g[x].size() ; ++i)
    {
        int y = g[x][i];
        if (mark[y]) continue;

        mark[y] = 1;
        df(y);
    }

    is.push(x);
}

void tdf(int x)
{
    r++;
    for (int i = 0 ; i < t[x].size() ; ++i)
    {
        int y = t[x][i];
        if (mark[y] == 0) continue;

        mark[y] = 0;
        tdf(y);
    }
}

int main()
{
freopen("ctc.in" , "r" , stdin);
freopen("ctc.out" , "w" , stdout);

p = 1;
//scanf("%d" , &p);
scanf("%d" , &n);
scanf("%d" , &m);

for (i = 1 ; i <= m ; ++i)
{
    scanf("%d" , &x);
    scanf("%d" , &y);

    g[x].push_back(y);
    t[y].push_back(x);
}

for (i = 1 ; i <= n ; ++i)
{
    if (mark[i]) continue;

    mark[i] = 1;
    df(i);
}


while (is.size())
{
    x = is.top();
    //cerr << x << '\n';
    is.pop();

    if (mark[x] == 0) continue;

    mark[x] = 0 , r = 0;
    tdf(x);
    //if (r >= 2)
    {
        cr++;
        mxr = max(mxr , r);
    }
}

if (p == 1) printf("%d\n" , cr);
else printf("%d\n" , mxr);

return 0;
}