Mai intai trebuie sa te autentifici.
Cod sursa(job #3160974)
| Utilizator | Data | 25 octombrie 2023 12:46:02 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.6 kb |
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
#define fi first
#define se second
#define pb push_back
ifstream f("dfs.in");
ofstream g("dfs.out");
const int N = 1e5+5;
int n, k;
vector<int> e[N];
bool b[N];
void read();
void dfs(int);
int main()
{
read();
for (int i = 1; i <= n; i++)
if (!b[i]) k++, dfs(i);
g << k;
return 0;
}
void read()
{
int m;
f >> n >> m;
while (m--){
int a, b; f >> a >> b;
e[a].pb(b); e[b].pb(a);
}
}
void dfs(int nod){
b[nod] = 1;
for (auto it: e[nod])
if (!b[it]) dfs(it);
}
