Skip to content

Commit c397ff9

Browse files
committed
Use stack_union and int in graphs instead of size_t
1 parent 04e623c commit c397ff9

File tree

5 files changed

+51
-22
lines changed

5 files changed

+51
-22
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef CP_ALGO_DATA_STRUCTURES_STACK_UNION_HPP
2+
#define CP_ALGO_DATA_STRUCTURES_STACK_UNION_HPP
3+
#include <cstddef>
4+
#include <vector>
5+
namespace cp_algo::data_structures {
6+
template<class datatype>
7+
struct stack_union {
8+
stack_union(size_t n): n(n), head(n), next(1), data(1) {}
9+
10+
void push(size_t v, datatype const& vdata) {
11+
next.push_back(head[v]);
12+
head[v] = size(next) - 1;
13+
data.push_back(vdata);
14+
}
15+
template<typename... Args>
16+
void emplace(size_t v, Args&&... vdata) {
17+
next.push_back(head[v]);
18+
head[v] = size(next) - 1;
19+
data.emplace_back(std::forward<Args...>(vdata...));
20+
}
21+
22+
size_t n;
23+
std::vector<size_t> head, next;
24+
std::vector<datatype> data;
25+
};
26+
}
27+
#endif // CP_ALGO_DATA_STRUCTURES_STACK_UNION_HPP

cp-algo/graph/base.hpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
11
#ifndef CP_ALGO_GRAPH_BASE_HPP
22
#define CP_ALGO_GRAPH_BASE_HPP
3+
#include "../data_structures/stack_union.hpp"
34
#include <iostream>
45
#include <ranges>
56
#include <vector>
67
namespace cp_algo::graph {
78
enum type {directed = 0, undirected = 1};
89
template<type undirected>
910
struct graph {
10-
graph(size_t n, size_t v0 = 0): n(n), v0(v0), adj(n) {}
11+
graph(int n, int v0 = 0): n(n), v0(v0), adj(n) {}
1112

12-
void add_edge(size_t u, size_t v) {
13-
adj[u - v0].push_back(size(to));
13+
void add_edge(int u, int v) {
14+
adj.push(u - v0, size(to));
1415
to.push_back(v - v0);
1516
if constexpr (undirected) {
16-
adj[v - v0].push_back(size(to));
17+
adj.push(v - v0, size(to));
1718
}
1819
to.push_back(u - v0);
1920
}
20-
void read_edges(size_t m) {
21-
for(size_t i = 0; i < m; i++) {
21+
void read_edges(int m) {
22+
for(int i = 0; i < m; i++) {
2223
int u, v;
2324
std::cin >> u >> v;
2425
add_edge(u, v);
2526
}
2627
}
27-
auto adjacent_generator(size_t v) const {
28-
return [&adj = adj[v], idx = 0]() mutable {
29-
return idx < ssize(adj) ? adj[idx++] : -1;
28+
auto adjacent_generator(int v) const {
29+
return [&adj = adj, v = adj.head[v]]() mutable {
30+
int e = v ? adj.data[v] : -1;
31+
v = adj.next[v];
32+
return e;
3033
};
3134
}
3235
auto nodes_view() const {
33-
return std::views::iota((size_t)0, n);
36+
return std::views::iota(0, n);
3437
}
3538

36-
size_t n;
37-
size_t v0;
38-
std::vector<size_t> to;
39-
std::vector<std::vector<size_t>> adj;
39+
int n, v0;
40+
std::vector<int> to;
41+
data_structures::stack_union<int> adj;
4042
};
4143
}
4244
#endif // CP_ALGO_GRAPH_BASE_HPP

cp-algo/graph/cycle.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
#include <algorithm>
55
#include <vector>
66
namespace cp_algo::graph {
7-
std::vector<size_t> find_cycle(auto const& g) {
7+
std::vector<int> find_cycle(auto const& g) {
88
std::vector<char> state(g.n);
9-
std::vector<size_t> cycle;
10-
auto dfs = [&](auto self, size_t v, size_t pe) -> bool {
9+
std::vector<int> cycle;
10+
auto dfs = [&](auto &&self, int v, int pe) -> bool {
1111
state[v] = 1;
1212
auto gen = g.adjacent_generator(v);
13-
for(size_t e = gen(); ~e; e = gen()) {
13+
for(int e = gen(); ~e; e = gen()) {
1414
if(e / 2 != pe / 2) {
1515
auto u = g.to[e];
1616
if(state[u] == 0) {
@@ -29,8 +29,8 @@ namespace cp_algo::graph {
2929
state[v] = 2;
3030
return false;
3131
};
32-
for(size_t i: g.nodes_view()) {
33-
if(!state[i] && dfs(dfs, i, -1)) {
32+
for(int i: g.nodes_view()) {
33+
if(!state[i] && dfs(dfs, i, -2)) {
3434
break;
3535
}
3636
}

verify/graph/cycle.test.cpp renamed to verify/graph/cycle_directed.test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @brief Cycle Detection (Directed)
22
#define PROBLEM "https://judge.yosupo.jp/problem/cycle_detection"
33
#pragma GCC optimize("Ofast,unroll-loops")
4-
//#pragma GCC target("tune=native")
4+
#pragma GCC target("tune=native")
55
#include "cp-algo/graph/cycle.hpp"
66
#include <bits/stdc++.h>
77

verify/graph/cycle_undirected.test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @brief Cycle Detection (Undirected)
22
#define PROBLEM "https://judge.yosupo.jp/problem/cycle_detection_undirected"
33
#pragma GCC optimize("Ofast,unroll-loops")
4-
//#pragma GCC target("tune=native")
4+
#pragma GCC target("tune=native")
55
#include "cp-algo/graph/cycle.hpp"
66
#include <bits/stdc++.h>
77

0 commit comments

Comments
 (0)