Untitled
unknown
plain_text
7 months ago
920 B
3
Indexable
Never
#include <bits/stdc++.h> using namespace std; typedef long long int ll; #define endl "\n" const double PI = 3.14159265358979; const ll INF = 1e18 + 7; const ll MOD = 1e9 + 7; const ll nax = 100005; const int LOG = 25; vector<int> adj[nax]; vector<int> vis(nax, 0); void dfs(int u) { vis[u] = 1; for (int v : adj[u]) { if (!vis[v]) { dfs(v); } } } void solve() { int n , m; cin >> n >> m; for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; adj[x].emplace_back(y); adj[y].emplace_back(x); } int components = 0; for (int i = 1; i <= n; i++) { if (vis[i]) continue; components++; dfs(i); } cout << components - 1; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); // int t; cin >> t; while(t--) solve(); return 0; }
Leave a Comment