forked from No-Idle/graph-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadjacencyclass.cpp
206 lines (177 loc) · 6.64 KB
/
adjacencyclass.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using namespace std;
namespace graphs {
struct graph {
struct vertex {
struct edge {
vertex *p;
int weight;
edge(vertex *p, int w) : p(p), weight(w) {}
bool operator<(const edge &e) const { return *p < *e.p; }
};
vector<edge> edges;
int n, component, up, depth, degree;
int x, y;
bool visited = false;
bool artpoint;
bool operator<(const vertex &v) const { return n < v.n; }
bool operator==(const vertex &v) const { return n == v.n; }
explicit vertex(int n = 0) : component(-1), n(n), depth(1e15), up(-1), degree(0) {}
void dfs() {
for (edge &e : edges) {
e.p->dfs();
}
}
void relax() {
for (edge e : edges) {
if (e.p->depth > depth + e.weight) e.p->depth = depth + e.weight;
}
}
void set_comp(int c) {
if (component != -1) return; else component = c;
for (edge &e : edges) e.p->set_comp(c);
}
void bfs() {
up = depth = 0;
queue<vertex *> queue;
queue.push(this);
while (!queue.empty()) {
vertex *v = queue.front();
queue.pop();
for (edge e : v->edges) {
if (e.p->depth != -1) continue;
queue.push(e.p);
e.p->up = e.p->depth = v->depth + 1;
}
}
}
void top_sort(vector<vertex *> &top_sorted) {
visited = true;
for (edge e : edges) {
if (!e.p->visited) e.p->top_sort(top_sorted);
}
top_sorted.push_back(this);
}
bool find(vertex *f, vector<vertex *> way) {
if (*this == *f) return true;
visited = true;
for (edge e : edges) {
if (!e.p->visited) {
if (e.p->find(f, way)) {
way.push_back(this);
return true;
}
}
}
return false;
}
void find_artpoints(vertex *parent) {
visited = true;
int count = 0;
for (edge e : edges) {
if (!e.p->visited) {
e.p->up = e.p->depth = depth + 1;
e.p->find_artpoints(this);
count++;
up = min(up, e.p->up);
degree = max(degree, e.p->up);
}
if (e.p != parent)
up = min(up, e.p->depth);
}
if ((parent != nullptr && degree >= depth && count > 0) || (parent == nullptr && count > 1))
this->artpoint = true;
}
friend ostream &operator<<(ostream &os, vertex *v) {
os << v->n + 1;
return os;
}
};
vector<vertex *> vertices;
int vertex_count, edge_count;
bool directed, weight;
explicit graph(int n, int m, bool directed = false, bool weight = false)
: vertices(n), vertex_count(n), edge_count(m), directed(directed), weight(weight) {
for (int i = 0; i < n; ++i) {
vertices[i] = new vertex(i);
}
}
inline vertex *operator[](int idx) { return vertices[idx]; }
void link(int i, int j, int w = 1) {
vertices[i]->edges.emplace_back(vertices[j], w);
if (!directed) vertices[j]->edges.emplace_back(vertices[i], w);
}
void create(int R, int x) {
for (vertex *v : vertices) v->edges.clear();
for (int i = 0; i < vertex_count; ++i) {
for (int j = i + 1; j < vertex_count; ++j) {
if (i == x || j == x) continue;
if (((vertices[i]->x - vertices[j]->x) * (vertices[i]->x - vertices[j]->x) +
(vertices[i]->y - vertices[j]->y) * (vertices[i]->y - vertices[j]->y)) <= R)
link(i, j);
}
}
}
friend istream &operator>>(istream &is, graph &G) {
for (int i = 0; i < G.edge_count; ++i) {
int u = 0, v = 0, w = 1;
cin >> u >> v;
if (G.weight) cin >> w;
G.link(u - 1, v - 1, w);
}
return is;
}
vector<vertex *> find_artpoints() {
for (vertex *v : vertices) v->visited = false;
for (vertex *v : vertices) {
if (!v->visited) {
v->up = v->depth = 0;
v->find_artpoints(nullptr);
}
}
vector<vertex *> artpoints = *new vector<vertex *>;
for (vertex *v : vertices) {
if (v->artpoint) artpoints.push_back(v);
}
return artpoints;
}
vector<vertex *> top_sort() {
for (vertex *v : vertices) v->visited = false;
vector<vertex *> top_sorted = *new vector<vertex *>;
for (vertex *v : vertices) {
if (!v->visited) {
v->top_sort(top_sorted);
}
}
reverse(top_sorted.begin(), top_sorted.end());
return top_sorted;
}
vector<vertex *> way(int s, int t) {
for (vertex *v : vertices) v->visited = false;
vector<vertex *> ans;
vertices[s]->find(vertices[t], ans);
return ans;
}
int comp_count() {
for (vertex *v : vertices) v->component = -1;
int c = 0;
for (vertex *v : vertices) {
if (v->component == -1) v->set_comp(c++);
}
return c;
}
};
struct tree : graph {
vertex *root;
explicit tree(int n, int root = 0, bool weight = false)
: graph(n, n - 1, true, weight), root(vertices[0]) {}
friend istream &operator>>(istream &is, tree &T) {
for (int i = 0; i < T.edge_count; ++i) {
int parent = 0, weight = 0;
cin >> parent;
if (weight) cin >> weight;
T[parent]->edges.emplace_back(T[i + 1], weight);
}
return is;
}
};
}