-
Notifications
You must be signed in to change notification settings - Fork 0
/
1260.cpp
54 lines (48 loc) · 913 Bytes
/
1260.cpp
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
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int N,M,V;
int graph[1001][1001];
bool visited[1001];
queue<int> q;
void BFS(int s){
q.push(s);
visited[s]=1;
cout << s << " ";
while(!q.empty()){
int v=q.front();
q.pop();
for(int i=1; i<=N;i++){
if(graph[v][i]==1&&visited[i]==0){
q.push(i);
visited[i]=1;
cout << i << " ";
}
}
}
}
void DFS(int s){
visited[s]=1;
cout << s << " ";
for(int i=1;i<=N;i++){
if(graph[s][i]==1&&visited[i]==0){
DFS(i);
}
}
}
void INIT(){
for(int i=1; i<=N; i++) visited[i]=0;
}
int main(){
cin >> N>>M>>V;
for(int i=0; i<M; i++){
int s,e; cin >> s>>e;
graph[s][e]=1;
graph[e][s]=1;
}
DFS(V);
cout << "\n";
INIT();
BFS(V);
}