-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbfs-dfs.cpp
78 lines (62 loc) · 1.22 KB
/
bfs-dfs.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <bits/stdc++.h>
#define _DEBUG
#define ll long long
#define pb push_back
#define REP(i, n) for (int i = 0; i < (int)n; i++)
using namespace std;
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b)
{
return (a.second < b.second);
}
// bool visited[128];
bitset<128> visited(0);
vector<vector<int>> graph(128);
vector<vector<pair<int,int>>> graph(128);
void dfs(int node) {
visited[node] = 1;
cout << node << endl;
for (int i = 0; i < graph[node].size(); i++) {
if (!visited[graph[node][i]]) {
dfs(graph[node][i]);
} else {
cout << graph[node][i] << " Seen before" << endl;
}
}
}
void solve() {
int n, m;
cin >> n >> m;
int from, to;
//normal
REP(i, m) {
cin >> from >> to;
graph[from].push_back(to);
}
for (int i = n; i >= 1; i--)
if (!visited[i])
dfs(i);
cout << endl;
visited.reset();
}
int main()
{
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int tt = clock();
#endif
ios_base::sync_with_stdio(false);
int no_of_test_cases;
//cin >> no_of_test_cases;
no_of_test_cases = 1;
while (no_of_test_cases--)
{
solve();
}
#ifdef _DEBUG
cerr << "TIME = " << clock() - tt << endl;
tt = clock();
#endif
return 0;
}
// TAG BFS / DFS