-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1260.cpp
More file actions
60 lines (50 loc) · 1.02 KB
/
1260.cpp
File metadata and controls
60 lines (50 loc) · 1.02 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
59
60
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
using namespace std;
#define MAXN 1050
int N,M,V;
bool checked[MAXN];
vector<int> adj[MAXN];
void DFS(int u){
checked[u] = true;
cout << u << " ";
for(auto v: adj[u]){
if(checked[v]) continue;
DFS(v);
}
}
void BFS(int u){
queue<int> q;
q.push(u);
checked[u] = true;
while(!q.empty()){
int top = q.front();
cout << top << " ";
q.pop();
for(auto v: adj[top]){
if(checked[v]) continue;
checked[v] = true;
q.push(v);
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin >> N >> M >> V;
for(int i=0 ; i<M ; i++){
int u,v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i=1 ; i<=N ; i++){
sort(adj[i].begin(), adj[i].end());
}
DFS(V);
for(int i=1 ; i<=N ; i++) checked[i] = false;
cout << '\n';
BFS(V);
}