Cod sursa(job #2774659)

Utilizator SerbaP123Popescu Serban SerbaP123 Data 12 septembrie 2021 10:48:31
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <bits/stdc++.h>
using namespace std;

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

#define cin in
#define cout out
#define pb push_back
#define f first
#define s second
#define FOR(i, a, b) for(int i = a; i <= b; ++i)

typedef long long ll;
typedef long double lld;
typedef unsigned long long ull;

const ll INF = 1e6;
const lld pi = 3.14159265358979323846;
const ll mod = 1000000007;
const ll nmax = 1e5;

ll n, m, x, y, z, i, j, k, l, r, p;
//ll v[nmax + 1];
ll a[nmax + 1];
//ll b[nmax + 1];
ll ans = 0, cnt = 0, maxim;
string s, t;
vector <int> edges[nmax + 1];

void DFS(int node){
	if(a[node]){
		return;
	}
	a[node] = true;
	for(auto nb : edges[node]){
		DFS(nb);
	}
}

void solve(int tc = 0){
	cin >> n >> m;
	while(m--){
		cin >> x >> y;
		edges[x].pb(y);
		edges[y].pb(x);
	}
	FOR(i, 1, n){
		if(!a[i]){
			ans++;
			DFS(i);
		}
	}
	cout << ans;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int tc = 1;
	//cin >> tc;
	FOR(i, 1, tc)
		solve(i);
}