Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <numeric>
- #include <cmath>
- #include <climits>
- #include <queue>
- using namespace std;
- const int N = 8e5, M = 4e6;
- int n, m, Trace[N+1], x, y;
- vector<vector<int>> Adj;
- void read()
- {
- cin >> n >> m;
- Adj.resize(n + 1);
- for (int i = 1; i <= m; ++ i)
- {
- cin >> x >> y;
- Adj[x].push_back(y);
- Adj[y].push_back(x);
- }
- }
- void DFS(int u)
- {
- Trace[u] = -1;
- for (int v : Adj[u])
- {
- if (Trace[v] == 0)
- {
- Trace[v] = u;
- DFS(v);
- }
- }
- }
- void solve()
- {
- int cnt = 0;
- for (int i = 0; i < n; ++ i)
- {
- if (Trace[i] == 0)
- {
- ++cnt;
- DFS(i);
- }
- }
- cout << cnt;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- //freopen ("TESTCODE.INP", "r", stdin);
- read();
- solve();
- }
Add Comment
Please, Sign In to add comment