-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS_graph.cpp
53 lines (43 loc) · 1.25 KB
/
DFS_graph.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 4*1e5+5;
#define all(x) x.begin(),x.end()
#define MOD 1000000007
#define pii pair<int,int>
#define pis pair<int,string>
#define F first
#define S second
#define LCM(a,b) ((a*b)/__gcd(a,b))
#define inf 1e15
#define test ll cse;cin>>cse;for(ll i=1;i<=cse;i++)
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define PI 3.14159265
#define MAXN 10000001
#define pb push_back
#define mp make_pair
vector<int> G[N];
bool used[N];
vector<int> ar;
void dfs(int u){
ar.pb(u);
used[u]=true;
for(int e: G[u]){
if(!used[e]) dfs(e);
}
if(G[u].size() > 1) ar.pb(u);
}
int main(){
fast;
int n,m; cin>>n>>m;
int x,y;
for(int i=1;i<=m;i++){
cin>>x>>y;
G[x].pb(y);
G[y].pb(x);
}
memset( used , false, sizeof(used));
dfs(1);
for(int e: ar) cout << e<<" "; cout <<"\n";
return 0;
}