Cod sursa(job #3150691)

Utilizator BOSSSTEFANPetrescu Ioan Stefan BOSSSTEFAN Data 17 septembrie 2023 23:17:30
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.64 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream cin("dfs.in");
ofstream cout("dfs.out");
int vf[100001];
struct ura
{
    vector<int> v;
}graf[100001];
void dfs(int x)
{
    vf[x]=1;
    for(int i=0;i<graf[x].v.size();i++)
        if(vf[graf[x].v[i]]==0)
            dfs(graf[x].v[i]);
}
int main()
{
    int n,m,i,x,y,rez=0;
    cin>>n>>m;
    for(i=1;i<=m;i++)
    {
        cin>>x>>y;
        graf[x].v.push_back(y);
        graf[y].v.push_back(x);
    }
    for(i=1;i<=n;i++)
    {
        if(vf[i]==0)
        {
            rez++;
            dfs(i);
        }
    }
    cout<<rez;
    return 0;
}