forked from No-Idle/graph-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightadjclass.cpp
60 lines (52 loc) · 1.27 KB
/
lightadjclass.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
#define WHITE 0
#define GRAY 1
#define BLACK 2
using namespace std;
constexpr size_t MAIN_SIZE = 64;
struct basic_vertex {
int x;
int used;
vector<Vertex *> neibh;
basic_vertex(int a) : x(a), used(WHITE)
{ neibh.reserve(MAIN_SIZE); }
};
class Vertex : public basic_vertex {
Vertex(int a) : basic_vertex(a)
{}
vector<int> weight;
Vertex *parent;
};
void bind(basic_vertex *a, basic_vertex *b) {
a->neibh.push_back(b);
b->neibh.push_back(a);
}
void dfs(basic_vertex *v) {
v->used = BLACK;
for (Vertex * &u : v->neibh)
if (u->used = WHITE)
dfs(u);
}
void bind_or(basic_vertex *a, basic_vertex *b) {
a->neibh.push_back(b);
}
void remove(basic_vertex *a, basic_vertex *b) {
auto i1 = find(a->neibh.begin(), a->neibh.end(), b);
auto i2 = find(b->neibh.begin(), b->neibh.end(), a);
a->neibh.erase(i1);
b->neibh.erase(i2);
}
void remove_or(basic_vertex *a, basic_vertex *b) {
a->neibh.erase(find(a->neibh.begin(), a->neibh.end(), b));
}
bool has_cycle(basic_vertex *v)
{
v->used = GRAY;
for (auto &u : v->neibh) {
if (u->used == WHITE) {
if (dfs(u)) return true;
} else if (u->used == GRAY)
return true;
}
v->used = BLACK;
return false;
}