-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgraph.cpp
More file actions
93 lines (75 loc) · 1.92 KB
/
graph.cpp
File metadata and controls
93 lines (75 loc) · 1.92 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
template<int V, bool DIRECTED>
struct UnweighedGraph {
vector<int> g[V];
void isVertex(int v) {
assert(0 <= v && v < V);
}
void clear(int to = V) {
assert(0 <= to && to <= V);
for (int i = 0; i < to; ++i) g[i].clear();
}
void readEdges(int m) {
while (m --> 0) {
int v, u; cin >> v >> u;
addEdge(v, u);
}
}
void addDirecteddEdge(int v, int u) {
isVertex(v); isVertex(u);
g[v].push_back(u);
}
void addBiEdge(int v, int u) {
isVertex(v); isVertex(u);
addDirecteddEdge(v, u);
addDirecteddEdge(u, v);
}
void addEdge(int v, int u) {
if (DIRECTED)
addDirecteddEdge(v, u);
else
addBiEdge(v, u);
}
vector<vector<int>> dfs() {
vector<vector<int>> comps;
vector<bool> visit(V, false);
void dfsInternal(int v) {
visit[v] = true;
for (int u : g[v]) if (!visit[u]) {
comps.back().push_back(u);
dfsInternal(u);
}
}
for (int v = 0; v < V; ++v) {
comps.push_back(vector<int>(1, v));
dfs(v);
}
return comps;
}
};
template<int maxv>
struct BFS {
typedef UnweighedGraph<maxv, false> Graph;
BFS(const Graph& g) : graph(g) {
}
vector<int> run(int start) {
assert(0 <= start && start < maxv);
vector<int> dist(maxv, -1);
queue<int> que;
que.push(start);
dist[start] = 0;
while (!que.empty()) {
const int v = que.front();
const int d = dist[v] + 1;
que.pop();
for (const int u : graph.g[v]) {
if (dist[u] == -1) {
dist[u] = d;
que.push(u);
}
}
}
return dist;
}
private:
const Graph graph;
};