Created
March 8, 2019 00:04
-
-
Save lawrencefmm/df454a31227fdbe6e1bfa6123b13a98f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
const int maxn = 1e5 + 10; | |
vector<int> G[maxn]; | |
bool vis[maxn]; | |
void connect(int a, int b) | |
{ | |
G[a].push_back(b); | |
G[b].push_back(a); | |
} | |
void dfs(int x) | |
{ | |
vis[x] = true; | |
cout << x << "\n"; | |
for(int u : G[x]) | |
{ | |
if(!vis[u]) dfs(u); | |
} | |
} | |
void bfs(int x) | |
{ | |
queue<int> q; | |
q.push(x); | |
while(!q.empty()) | |
{ | |
int atual = q.front(); | |
q.pop(); | |
vis[atual] = true; | |
cout << atual << "\n"; | |
for(int u : G[atual]) | |
{ | |
if(!vis[u]) q.push(u); | |
} | |
} | |
} | |
int main() | |
{ | |
int n, m; | |
cin >> n >> m; | |
for(int i = 0; i < m; i++) | |
{ | |
int a, b; | |
cin >> a >> b; | |
connect(a, b); | |
} | |
bfs(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment