Cod sursa(job #2176294)

Utilizator ARobertAntohi Robert ARobert Data 16 martie 2018 22:19:22
Problema Componente tare conexe Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <bits/stdc++.h>

using namespace std;

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

int n,m,x,y,viz[100001],c;
vector<int> g[100001], gt[100001];
stack<int>s;

void dfs(int nod)
{
    viz[nod]=1;
    for (auto v: g[nod])
        if (!viz[v])
        dfs(v);
    s.push(nod);
}

void dfsr(int nod)
{
    viz[nod]=1;
    for (auto v :gt[nod])
        if (!viz[v])
        dfsr(v);
}

int main()
{
    fin>>n>>m;
    for (int i=1;i<=m;i++)
    {
        fin>>x>>y;
        g[x].push_back(y);
        gt[y].push_back(x);
    }
    for (int i=1;i<=n;i++)
        if (!viz[i])
        dfs(i);
    memset(viz,0,sizeof(viz));
    while (!s.empty())
    {
        int t=s.top();
        s.pop();
        if (!viz[t])
            dfsr(t), c++;
    }
    fout<<c;
    return 0;
}