-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
vector<int> par, dsu_2ecc, dsu_cc, dsu_cc_size; | ||
int bridges; | ||
int lca_iteration; | ||
vector<int> last_visit; | ||
|
||
void init(int n) { | ||
par.resize(n); | ||
dsu_2ecc.resize(n); | ||
dsu_cc.resize(n); | ||
dsu_cc_size.resize(n); | ||
lca_iteration = 0; | ||
last_visit.assign(n, 0); | ||
for (int i=0; i<n; ++i) { | ||
dsu_2ecc[i] = i; | ||
dsu_cc[i] = i; | ||
dsu_cc_size[i] = 1; | ||
par[i] = -1; | ||
} | ||
bridges = 0; | ||
} | ||
|
||
int find_2ecc(int v) { | ||
if (v == -1) | ||
return -1; | ||
return dsu_2ecc[v] == v ? v : dsu_2ecc[v] = find_2ecc(dsu_2ecc[v]); | ||
} | ||
|
||
int find_cc(int v) { | ||
v = find_2ecc(v); | ||
return dsu_cc[v] == v ? v : dsu_cc[v] = find_cc(dsu_cc[v]); | ||
} | ||
|
||
void make_root(int v) { | ||
int root = v; | ||
int child = -1; | ||
while (v != -1) { | ||
int p = find_2ecc(par[v]); | ||
par[v] = child; | ||
dsu_cc[v] = root; | ||
child = v; | ||
v = p; | ||
} | ||
dsu_cc_size[root] = dsu_cc_size[child]; | ||
} | ||
|
||
void merge_path (int a, int b) { | ||
++lca_iteration; | ||
vector<int> path_a, path_b; | ||
int lca = -1; | ||
while (lca == -1) { | ||
if (a != -1) { | ||
a = find_2ecc(a); | ||
path_a.push_back(a); | ||
if (last_visit[a] == lca_iteration){ | ||
lca = a; | ||
break; | ||
} | ||
last_visit[a] = lca_iteration; | ||
a = par[a]; | ||
} | ||
if (b != -1) { | ||
b = find_2ecc(b); | ||
path_b.push_back(b); | ||
if (last_visit[b] == lca_iteration){ | ||
lca = b; | ||
break; | ||
} | ||
last_visit[b] = lca_iteration; | ||
b = par[b]; | ||
} | ||
|
||
} | ||
|
||
for (int v : path_a) { | ||
dsu_2ecc[v] = lca; | ||
if (v == lca) | ||
break; | ||
--bridges; | ||
} | ||
for (int v : path_b) { | ||
dsu_2ecc[v] = lca; | ||
if (v == lca) | ||
break; | ||
--bridges; | ||
} | ||
} | ||
|
||
void add_edge(int a, int b) { | ||
a = find_2ecc(a); | ||
b = find_2ecc(b); | ||
if (a == b) | ||
return; | ||
|
||
int ca = find_cc(a); | ||
int cb = find_cc(b); | ||
|
||
if (ca != cb) { | ||
++bridges; | ||
if (dsu_cc_size[ca] > dsu_cc_size[cb]) { | ||
swap(a, b); | ||
swap(ca, cb); | ||
} | ||
make_root(a); | ||
par[a] = dsu_cc[a] = b; | ||
dsu_cc_size[cb] += dsu_cc_size[a]; | ||
} else { | ||
merge_path(a, b); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions
17
resources/Code/Math/NumberTheory/ChineseRemainderTheorem.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
struct Congruence { | ||
long long a, m; | ||
}; | ||
long long chinese_remainder_theorem(vector<Congruence> const& congruences) { | ||
long long M = 1; | ||
for (auto const& congruence : congruences) { | ||
M *= congruence.m; | ||
} | ||
long long solution = 0; | ||
for (auto const& congruence : congruences) { | ||
long long a_i = congruence.a; | ||
long long M_i = M / congruence.m; | ||
long long N_i = mod_inv(M_i, congruence.m); | ||
solution = (solution + a_i * M_i % M * N_i) % M; | ||
} | ||
return solution; | ||
} |