-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1260.cpp
52 lines (45 loc) · 1.18 KB
/
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
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
vector<vector<int>> graph(1001,vector<int>());
int n,m,v,a,b;
bool visited[2][1001];
queue<int> q;
stack<int> s;
int main() {
scanf("%d%d%d",&n,&m,&v);
for(int i=0;i<m;++i){
scanf("%d%d",&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());
graph[i].erase(unique(graph[i].begin(), graph[i].end()), graph[i].end());
}
s.push(v);
while(!s.empty()){
int ver = s.top(); s.pop();
if(visited[0][ver]) continue;
visited[0][ver] = true;
printf("%d ", ver);
for(auto it=graph[ver].rbegin(); it!=graph[ver].rend(); ++it)
if(!visited[0][*it])
s.push(*it);
}
printf("\n");
q.push(v);
while(!q.empty()){
int ver = q.front(); q.pop();
if(visited[1][ver]) continue;
visited[1][ver] = true;
printf("%d ", ver);
for(auto it=graph[ver].begin();it!=graph[ver].end();++it)
if(!visited[1][*it])
q.push(*it);
}
return 0;
}