-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathicpc1260.cpp
More file actions
52 lines (49 loc) · 1.1 KB
/
icpc1260.cpp
File metadata and controls
52 lines (49 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <vector>
#include <queue>
#include <set>
using namespace std;
void dfs(int u, vector<set<int> > &graph, vector<bool> &visited){
visited[u] = true;
cout << u << ' ';
for(int v : graph[u])
if(!visited[v])
dfs(v, graph, visited);
}
void bfs(int s, vector<set<int> > &graph){
queue<int> que;
vector<bool> visited(graph.size());
visited[s] = true;
que.push(s);
while(que.size()){
int u = que.front();
cout << u << ' ';
que.pop();
for(int v : graph[u])
if(!visited[v]){
visited[v] = true;
que.push(v);
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m, v;
vector<bool> visited;
vector<set<int> > graph;
cin >> n >> m >> v;
visited.resize(n + 1);
graph.resize(n + 1);
for(int i = 0; i < m; i++){
int s, t;
cin >> s >> t;
graph[s].insert(t);
graph[t].insert(s);
}
dfs(v, graph, visited);
cout << '\n';
bfs(v, graph);
return 0;
}