-
Notifications
You must be signed in to change notification settings - Fork 0
/
rand.cc
168 lines (153 loc) · 4.46 KB
/
rand.cc
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
mt19937_64 rnd_ = mt19937_64(random_device()());
template<typename T>
inline T randint(T mn, T mx) { // [mn, mx]
assert(mn <= mx);
return uniform_int_distribution<T>(mn, mx)(rnd_);
}
template<typename T>
vector<T> random_vector(int n, T mn, T mx) { // [mn, mx]
/* O(n) */
assert(mn <= mx);
uniform_int_distribution<T> dist(mn, mx);
vector<T> a(n);
for(int i=0; i<n; ++i) a[i] = dist(rnd_);
return a;
}
template<typename T>
vector<T> random_vector_without_duplicates(int n, T mn, T mx) { // [mn, mx]
/* O(n) */ // Fisher-Yates
assert(mn <= mx);
vector<T> a(n);
unordered_map<T,T> m;
T r = mx - mn;
for(int i=0; i<n; ++i,--r) {
T k = randint(T(0), r);
a[i] = (m.count(k) ? m[k] : k) + mn;
m[k] = (m.count(r) ? m[r] : r);
}
return a;
}
template<typename T>
vector<T> random_permutation(T mn, T mx) { // [mn, mx]
/* O(n) */
assert(mn <= mx);
vector<T> a(mx - mn + 1);
iota(a.begin(),a.end(),mn);
shuffle(a.begin(), a.end(), rnd_);
return a;
}
template<typename T>
pair<T,T> random_pair(T mn, T mx, bool allow_same) { // [mn, mx]
/* O(1) */
assert(mn <= mx);
T a = randint(mn, mx), b;
if(allow_same) {
b = randint(mn, mx);
}
else {
assert(mn != mx);
b = randint(mn, mx-1);
if(a == b) b = mx;
}
return {a, b};
}
template<typename T>
pair<T,T> random_range(T mn, T mx, bool allow_same) { // [mn, mx]
/* O(1) */
assert(mn <= mx);
T l,r;
if(allow_same) {
l = randint(mn, mx);
r = randint(l, mx);
}
else {
assert(mn != mx);
l = randint(mn, mx-1);
r = randint(l+1, mx);
}
return {l, r};
}
vector<pair<int,int>> random_graph(int n, int m) {
/* O(n^2) */
assert(n >= 0);
assert(m >= 0);
assert(m <= n*(n-1)/2);
vector<pair<int,int>> edges; // 0-indexed edge list
edges.reserve(m);
int r = (n*(n-1) / 2) - 1;
for(int a=0; a<n; ++a) for(int b=a+1; b<n; ++b)
if(randint(0, r--) < m - ssize(edges)) edges.emplace_back(a,b);
assert(ssize(edges) == m);
return edges;
}
vector<pair<int,int>> random_path_graph(int n) {
/* O(n) */
vector<pair<int,int>> edges(n-1); // 0-indexed edge list
vector<int> p = random_permutation(0, n-1);
for(int i=0; i<n-1; ++i) edges[i] = {p[i], p[i+1]};
return edges;
}
vector<pair<int,int>> random_star_graph(int n) {
/* O(n) */
vector<pair<int,int>> edges; // 0-indexed edge list
edges.reserve(n-1);
int center = randint(0, n-1);
for(int i=0; i<n; ++i) if(i!=center) edges.emplace_back(center,i);
return edges;
}
vector<pair<int,int>> random_tree(int n) {
/* O(n log n) */
if(n==1) return {};
vector<pair<int,int>> edges; // 0-indexed edge list
edges.reserve(n-1);
vector<int> prufer_sequence(n-2);
for(int i=0; i<n-2; ++i) prufer_sequence[i] = randint(0, n-1);
vector<int> count(n, 1);
for(int v : prufer_sequence) count[v]++;
priority_queue<int, vector<int>, greater<int>> q;
for(int v=0; v<n; ++v) if(count[v]==1) q.push(v);
while(prufer_sequence.size()) {
int v= q.top(); q.pop();
int u = prufer_sequence.back(); prufer_sequence.pop_back();
edges.emplace_back(u,v);
count[u]--, count[v]--;
if(count[u] == 1) q.push(u);
}
vector<int> nonzero;
for(int v=0; v<n; ++v) if(count[v]) nonzero.push_back(v);
assert(nonzero.size() == 2);
edges.emplace_back(nonzero[0], nonzero[1]);
assert(ssize(edges) == n-1);
return edges;
}
vector<pair<int,int>> random_connected_graph(int n, int m) {
/* O(n^2) */
assert(n >= 0);
assert(m >= 0);
assert(m <= n*(n-1)/2);
assert(m >= n-1);
vector<pair<int,int>> edges = random_tree(n); // 0-indexed edge list
edges.reserve(m);
vector<vector<bool>> check(n, vector<bool>(n, false));
for(int i=0; i<n-1; ++i) {
int u = edges[i].first, v = edges[i].second;
check[u][v] = check[v][u] = true;
}
int required = m - ssize(edges);
int candidates = (n*(n-1) / 2) - ssize(edges);
int r = candidates - 1;
for(int a=0; a<n; ++a) for(int b=a+1; b<n; ++b) if(!check[a][b])
if(randint(0, r--) < required) edges.emplace_back(a,b), required--;
assert(ssize(edges) == m);
return edges;
}
const string string_digits = "0123456789";
const string string_alphabet_lowercase = "abcdefghijklmnopqrstuvwxyz";
const string string_alphabet_uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string random_string(int n, const string &population = string_digits + string_alphabet_lowercase + string_alphabet_uppercase) {
/* O(n) */
string r; r.reserve(n);
int sz = ssize(population);
for(int i=0; i<n; ++i) r.push_back(population[randint(0, sz-1)]);
return r;
}