Skip to content
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

Augmenting LinkCutTree doesn't seem to work #117

Open
tyilo opened this issue May 14, 2019 · 3 comments
Open

Augmenting LinkCutTree doesn't seem to work #117

tyilo opened this issue May 14, 2019 · 3 comments
Labels

Comments

@tyilo
Copy link
Contributor

tyilo commented May 14, 2019

I have tried to augment LinkCutTree with the subtree size, however I can't get it to work.

I have added int sz = 1; as a field to Node and modified Node::fix() to contain:

void fix() {
	sz = 1;
	if (c[0]) {
		c[0]->p = this;
		sz += c[0]->sz;
	}
	if (c[1]) {
		c[1]->p = this;
		sz += c[1]->sz;
	}
	// (+ update sum of subtree elements etc. if wanted)
}

My test code is

LinkCut T(4);
T.link(1, 0);
T.link(2, 1);
T.link(3, 0);

rep(i, 0, 4) {
	cout << i << " " << T.node[i].sz << "\n";
}

I would expect the output to be:

0 4
1 2
2 1
3 1

But instead I get:

0 2
1 1
2 1
3 1

The full code is available at https://gist.github.com/Tyilo/ebbbd06dcb02665c078ec3dac387c09a

@simonlindholm
Copy link
Member

Ugh. I don't understand the link-cut tree well enough to say what's going wrong. We should replace it by a better version... (#63)

@Chillee
Copy link
Collaborator

Chillee commented May 14, 2019

In general, I think for data structures that can be augmented, like segtrees or (as we recently learned) Link Cut Trees, we should try to separate the code that's meant to be augmented from the rest of the code.

@Altynai
Copy link

Altynai commented May 27, 2022

@tyilo you might need to maintain the size of virtual children as well, see Benq's implementation: LCT.h#L27, example problem: https://codeforces.com/contest/1681/problem/F

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants