-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdouble_dfs.cpp
More file actions
108 lines (100 loc) · 1.67 KB
/
Copy pathdouble_dfs.cpp
File metadata and controls
108 lines (100 loc) · 1.67 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <stack>
#include <unordered_set>
#include <vector>
#include <cstdlib>
#include <fstream>
using namespace std;
const int MAX_V = 100000;
stack<int> stack1, stack2;
unordered_set<int> hash1, hash2;
unordered_multiset<int> hstack1;
vector<int> s;
unordered_set<int> f;
vector<vector<int>> graph(MAX_V, vector<int>());
void dfs2(int &s1)
{
stack2.push(s1);
hash2.insert(s1);
for (auto &t : graph[s1])
{
if (hstack1.find(t) != hstack1.end())
{
cout << "Not empty" << endl;
cout << t;
cout << " ";
while (!stack2.empty())
{
cout << stack2.top();
cout << " ";
stack2.pop();
}
cout << endl;
while (!stack1.empty())
{
cout << stack1.top();
cout << " ";
stack1.pop();
}
cout << endl;
exit(0);
}
else if (hash2.find(t) == hash2.end())
{
dfs2(t);
}
}
stack2.pop();
}
void dfs1(int &s1)
{
stack1.push(s1);
hstack1.insert(s1);
hash1.insert(s1);
for (auto &t : graph[s1])
{
if (hash1.find(t) == hash1.end())
{
dfs1(t);
}
}
if (f.find(s1) != f.end())
{
dfs2(s1);
}
stack1.pop();
hstack1.erase(s1);
}
int main()
{
ifstream in("./input/double_dfs4.txt");
cin.rdbuf(in.rdbuf());
int e;
cin >> e;
for (int i = 0; i < e; i++)
{
int s, t;
cin >> s >> t;
graph[s].push_back(t);
}
int ns, nf;
cin >> ns;
int tm;
for (int i = 0; i < ns; i++)
{
cin >> tm;
s.push_back(tm);
}
cin >> nf;
for (int i = 0; i < nf; i++)
{
cin >> tm;
f.insert(tm);
}
for (auto &s1 : s)
{
dfs1(s1);
}
cout << "Empty" << endl;
return 0;
}