-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
58 lines (51 loc) · 1.14 KB
/
Copy pathmain.cpp
File metadata and controls
58 lines (51 loc) · 1.14 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
53
54
55
56
57
58
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
int N, M, V;
vector<int> graph[1001];
bool visited[1001];
void dfs(int node) {
visited[node] = true;
printf("%d ", node);
for(int i=0; i<graph[node].size(); i++) {
int next = graph[node][i];
if(!visited[next])
dfs(next);
}
}
void bfs(int start) {
queue<int> q;
memset(visited, false, sizeof(visited));
visited[start] = true;
q.push(start);
while(!q.empty()) {
int node = q.front();
q.pop();
printf("%d ", node);
for(int i=0; i<graph[node].size(); i++) {
int next = graph[node][i];
if(visited[next]) continue;
visited[next] = true;
q.push(next);
}
}
}
int main() {
scanf("%d %d %d", &N, &M, &V);
for(int i=0; i<M; i++) {
int u, v;
scanf("%d %d", &u, &v);
graph[u].push_back(v);
graph[v].push_back(u);
}
for(int i=1; i<=N; i++)
sort(graph[i].begin(), graph[i].end());
dfs(V);
puts("");
bfs(V);
puts("");
return 0;
}