-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1260.cpp
48 lines (41 loc) · 846 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
#include <cstdio>
#include <queue>
using namespace std;
int n, m, v;
int visit[1001];
int adj[1001][1001];
void dfs(int x){
visit[x]=true;
printf("%d ", x);
for(int i=1;i<=n;i++){
if(adj[x][i]==1 && !visit[i]) dfs(i);
}
}
void bfs(int s){
queue<int> q;
q.push(s);
visit[s]=false;
while(!q.empty()){
int x=q.front();
printf("%d ", x);
q.pop();
for(int i=1;i<=n;i++){
if(adj[x][i]==1 && visit[i]){ // visit를 반대로 활용 (true: 접속 안함)
q.push(i);
visit[i]=false;
}
}
}
}
int main(){
int a, b;
scanf("%d%d%d", &n, &m, &v);
for(int i=0;i<m;i++){
scanf("%d%d", &a, &b);
adj[a][b]=1; adj[b][a]=1;
}
dfs(v);
printf("\n");
bfs(v);
printf("\n");
}