Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- int n, m;
- vector<int> graph[105];
- bool visited[105];
- void dfs(int node) {
- visited[node] = true;
- cout << node << endl;
- for(int i = 0; i < graph[node].size(); i++) {
- int sosed = graph[node][i];
- if(!visited[sosed]) {
- dfs(sosed);
- }
- }
- }
- int main()
- {
- cin >> n >> m;
- for(int i = 0; i < n; i++) {
- visited[i] = false;
- }
- for(int i = 0; i < m; i++) {
- int a, b;
- cin >> a >> b;
- graph[a].push_back(b);
- graph[b].push_back(a);
- }
- dfs(0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement