Skip to content

Instantly share code, notes, and snippets.

@ArthurLoboLobo
Created February 20, 2023 14:07
Show Gist options
  • Save ArthurLoboLobo/6e4af6bd81b185f9085060cd5de2c7de to your computer and use it in GitHub Desktop.
Save ArthurLoboLobo/6e4af6bd81b185f9085060cd5de2c7de to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+10;
int n, m, c, visitado[MAXN];
vector<int> viz[MAXN];
void dfs(int a) {
visitado[a] = 1;
for(auto b : viz[a]) {
if(visitado[b] == 0) dfs(b);
}
}
int32_t main() {
cin >> n >> m;
for(int i = 1; i <= m; i++) {
int a,b; cin >> a >> b;
viz[a].push_back(b);
viz[b].push_back(a);
}
for(int i = 1; i <= n; i++) {
if(!visitado[i]) {
c++;
dfs(i);
}
}
cout << m-n+c << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment