Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- // n: núm. de nodos - m: núm. de aristas
- int n, m; cin >> n >> m;
- vector<vector<int>> adj(n+1);
- while (m--) {
- int u, v; cin >> u >> v;
- adj[u].push_back(v);
- adj[v].push_back(u);
- }
- vector<bool> vis(n+1);
- function<void(int)> dfs = [&](int u) {
- vis[u] = true;
- cout << u << ' ';
- for (int& v : adj[u]) {
- if (vis[v]) continue;
- dfs(v);
- }
- };
- for (int u = 1; u <= n; u++) {
- if (vis[u]) continue;
- dfs(u);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement