Cod sursa(job #2779575)
Utilizator | Data | 4 octombrie 2021 11:17:38 | |
---|---|---|---|
Problema | Parcurgere DFS - componente conexe | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.43 kb |
#include<bits/stdc++.h>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int n,m,i,x,y,c;
vector<int> v[100001];
bool z[100001];
void D(int x)
{
z[x]=1;
for(auto y:v[x])
if(!z[y])
D(y);
}
int main()
{
f>>n>>m;
while(m--)
f>>x>>y,v[x].push_back(y),v[y].push_back(x);
for(i=1;i<=n;++i)
if(!z[i])
++c,D(i);
g<<c;
return 0;
}