forked from kth-competitive-programming/kactl
-
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.
Directed MST reconstruction (kth-competitive-programming#153)
* UnionFind with rollback * Directed MST reconstruction * Include UnionFindRollback in the pdf, remove UnionFind Its functionality is a superset, and one should know UnionFind by heart anyway.
- Loading branch information
1 parent
4a147ce
commit fc1a059
Showing
5 changed files
with
67 additions
and
19 deletions.
There are no files selected for viewing
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,33 @@ | ||
/** | ||
* Author: Lukas Polacek, Simon Lindholm | ||
* Date: 2019-12-26 | ||
* License: CC0 | ||
* Source: folklore | ||
* Description: Disjoint-set data structure with undo. | ||
* If undo is not needed, skip st, time() and rollback(). | ||
* Usage: int t = uf.time(); ...; uf.rollback(t); | ||
* Time: $O(\log(N))$ | ||
*/ | ||
#pragma once | ||
|
||
struct RollbackUF { | ||
vi e; vector<pii> st; | ||
RollbackUF(int n) : e(n, -1) {} | ||
int size(int x) { return -e[find(x)]; } | ||
int find(int x) { return e[x] < 0 ? x : find(e[x]); } | ||
int time() { return sz(st); } | ||
void rollback(int t) { | ||
for (int i = time(); i --> t;) | ||
e[st[i].first] = st[i].second; | ||
st.resize(t); | ||
} | ||
bool join(int a, int b) { | ||
a = find(a), b = find(b); | ||
if (a == b) return false; | ||
if (e[a] > e[b]) swap(a, b); | ||
st.push_back({a, e[a]}); | ||
st.push_back({b, e[b]}); | ||
e[a] += e[b]; e[b] = a; | ||
return true; | ||
} | ||
}; |
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
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
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