-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1260.cpp
More file actions
71 lines (71 loc) · 1.68 KB
/
Copy path1260.cpp
File metadata and controls
71 lines (71 loc) · 1.68 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
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#define MAX 1001
using namespace std;
void dfs(int start, vector<int> graph[], bool check[]){
stack<int> s;
s.push(start);
check[start] = true;
cout << start ;
bool flag = false;
while(!s.empty()){
int current_node = s.top();
s.pop();
for(int i=0;i<graph[current_node].size();i++){
int next_node = graph[current_node][i];
if(check[next_node] == false){
cout <<" "<<next_node;
check[next_node] = true;
s.push(current_node);
s.push(next_node);
break;
}
}
}
}
void bfs(int start, vector<int> graph[], bool check[]){
queue<int> q;
q.push(start);
check[start] = true;
bool flag = false;
while(!q.empty()){
int tmp = q.front();
q.pop();
if(flag){
cout << " ";
}else{
flag =true;
}
cout <<tmp;
for(int i = 0; i< graph[tmp].size();i++){
if(check[graph[tmp][i]] == false){
q.push(graph[tmp][i]);
check[graph[tmp][i]] = true;
}
}
}
}
int main(){
int N, M, V;
cin >> N >> M >> V;
int A,B;
vector<int> graph[MAX];
bool check[MAX];
fill(check, check+N+1,false);
for(int i=0;i<M;i++){
cin >> A >> B;
graph[A].push_back(B);
graph[B].push_back(A);
}
for(int i=1;i<=N;i++){
sort(graph[i].begin(),graph[i].end());
}
dfs(V, graph,check);
fill(check, check+N+1,false);
cout <<endl;
bfs(V, graph,check);
return 0;
}