-
Notifications
You must be signed in to change notification settings - Fork 748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Link Cut Tree #124
base: main
Are you sure you want to change the base?
Link Cut Tree #124
Changes from 10 commits
e6aa29b
2d00e3b
805cc15
1a03c46
7887f5e
1c30800
93f1be2
06b3bd5
5dc6f51
0216a13
6fbfd26
673c2cc
613a162
1fdda13
e95227d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* Author: | ||
* Description: link-cut Tree. Supports BST-like | ||
* augmentations. (Can be used in place of HLD). | ||
* Current implementation supports update value at a node, | ||
* and query max on a path. | ||
* Tested on: http://acm.timus.ru/problem.aspx?num=1553 | ||
* Status: Passes existing fuzz tests (with function names modified). | ||
*/ | ||
struct Node { | ||
bool flip = 0; | ||
Node *pp, *p, *c[2]; // path parent, parent, children | ||
// add stuff | ||
int val = 0, cval = 0, ind; | ||
Node() { pp = p = c[0] = c[1] = 0; } | ||
void push() { | ||
if (flip) { | ||
rep(i, 0, 2) if(c[i]) c[i]->flip ^= 1; | ||
swap(c[0], c[1]); flip = 0; | ||
} | ||
} | ||
void pull() { | ||
cval = val; | ||
rep(i, 0, 2) if(c[i]) c[i]->push(), cval = max(cval, c[i]->cval); | ||
} | ||
void rot(bool t) { | ||
Node *y = p, *z = y->p; | ||
if (z) z->c[z->c[1] == y] = this; | ||
y->c[!t] = c[t]; | ||
if (c[t]) c[t]->p = y; | ||
c[t] = y; p = z; | ||
y->p = this; | ||
y->pull(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is an unreadable mess, it wouldn't hurt to golf it a bit and make it even more unreadable... some ideas:
Also, I'm a bit surprised the pull for z happens before that of y, I would have expected the other way around. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I might be misunderstanding, but why *&w instead of *w?
Like the fix function in the previous lct? (Set a node's children, and then fix will set those children's parents, but without the "(+ update sum of subtree elements etc. if wanted)" part, since that is what pull does). Pull for z was unnecessary. (A node is only moved up one level for each call of rot, and then pulls up itself at the end of splay.) Thanks for noticing. Everything else seems good. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, I confused
Yeah. I don't know much about how the previous lct works, but it seems like a method along those lines may be able to cut away a few lines? Again, I'm just throwing ideas out, I don't know whether it actually does save anything. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't look like adding fix will shorten it since |
||
} | ||
void xiao() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that we should probably rename this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "propogatePathParent" is a little much though... Will keep thinking about possible alternatives. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could certainly shorten that to |
||
if (p) p->xiao(), pp = p->pp; | ||
push(); | ||
} | ||
void splay() { | ||
xiao(); | ||
while (p) { | ||
Node* y = p; Node *z = y->p; | ||
bool t1 = (y->c[1] != this); | ||
if (z) { | ||
bool t2 = (z->c[1] != y); | ||
if (t1 == t2) y->rot(t2), rot(t1); | ||
else rot(t1), rot(t2); | ||
} else rot(t1); | ||
PotatoHashing marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
pull(); | ||
} | ||
Node* access() { | ||
for (Node *y = 0, *z = this; z; y = z, z = z->pp) { | ||
z->splay(); | ||
if (z->c[1]) z->c[1]->pp = z, z->c[1]->p = 0; | ||
z->c[1] = y; | ||
if (y) y->p = z; | ||
z->pull(); | ||
} | ||
splay(); | ||
return this; | ||
} | ||
Node* makeRoot() { | ||
access()->flip ^= 1; | ||
return this; | ||
} | ||
}; | ||
struct LinkCut { | ||
vector<Node> nodes; | ||
LinkCut(int N) : nodes(N) { rep(i, 0, N) nodes[i].ind = i; } | ||
int findRoot(int u) { | ||
Node *x = nodes[u].access(); | ||
while(x->c[0]) x = x->c[0]; | ||
return x->ind; | ||
} | ||
bool cut(int u, int v) { /// start-hash | ||
Node *y = nodes[v].makeRoot(); | ||
Node *x = nodes[u].access(); | ||
if (x->c[0] != y || y->c[1]) | ||
return false; | ||
x->c[0] = y->p = y->pp = 0; | ||
x->pull(); | ||
return true; | ||
} /// end-hash | ||
bool isConnected(int u, int v) { return findRoot(u) == findRoot(v); } | ||
bool link(int u, int v) { | ||
if (isConnected(u, v)) return false; | ||
nodes[u].makeRoot()->pp = &nodes[v]; | ||
return true; | ||
} | ||
void update(int u, int c) { | ||
nodes[u].access()->val += c; | ||
} | ||
int query(int u, int v) { // Find max on the path from u to v. | ||
nodes[v].makeRoot(); | ||
return nodes[u].access()->cval; | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll want to remove the old link-cut tree, so feel free to do that and modify the fuzz-test. Will also need to update the test to verify that max works (with max replaced by some non-commutative, non-associative function)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the fuzz-test to verify max for now. Will update it later for any new function that we decide on...