Cod sursa(job #1791350)

Utilizator medicinedoctoralexandru medicinedoctor Data 29 octombrie 2016 11:47:56
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream cin("dfs.in");
ofstream cout("dfs.out");

vector <vector <int> > a;
vector <bool> d;

void read()
{
    int n,m,x,y;
    cin >> n >> m;
    a.resize(n+1);
    for ( ; m ; m--)
    {
        cin >> x >> y;
        if (x!=y)
        {
            a[x].push_back(y);
            a[y].push_back(x);
        }
    }
    d.resize(n+1);
    fill(d.begin(),d.end(),true);
}

void solve()
{
    vector <int> q,zero;
    int x=1,y,c,mx=0;
    bool b;
    do
    {
        c=0;
        q.push_back(x);
        d[x]=false;
        for (int i=0; i<q.size(); i++)
        {
            for (int j=0; j<a[q[i]].size(); j++)
            {
                y=a[q[i]][j];
                if (d[y])
                {
                    q.push_back(y);
                    d[y]=false;
                    c++;
                }
            }
        }
        if (c>mx) mx=c;
        b=false;
        for (int i=1; i<d.size(); i++)
        {
            if (d[i])
            {
                b=true; x=i; i=d.size();
            }
        }
        q=zero;
    }
    while (b);
    cout << mx+1 << '\n';
}

main()
{
    read();
    solve();
}