-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeadlock.cpp
More file actions
51 lines (42 loc) · 894 Bytes
/
Copy pathdeadlock.cpp
File metadata and controls
51 lines (42 loc) · 894 Bytes
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
#include <set>
#include <vector>
#include <iostream>
using namespace std;
struct Visitor
{
Visitor(std::vector<std::vector<int>>& _c):c(_c){}
std::vector<std::vector<int>> c;
set<int> visited;
bool visit(int i);
};
bool hasDeadlock(std::vector<std::vector<int>> c)
{
cout << "test ";
Visitor v(c);
for(int i = 0; i < c.size(); ++i)
{
if (v.visit(i) == true)
return true;
}
}
bool Visitor::visit(int i)
{
if (visited.find(i) != visited.end())
return true;
visited.insert(i);
for (int j: c[i])
{
if (visit(j) ==true)
return true;
}
visited.erase(i);
return false;
}
int main(void)
{
std::vector<std::vector<int>> v = {{1,2,3},
{2,3},
{3},
{}};
hasDeadlock(v) ? cout << "True" : cout << "False";
}