# Algorithms Thread Episode 8: Tree Basics

Episode 8 of Algorithms Thread comes out in <90 minutes! This one is a bit more beginner-friendly and covers the following ideas:

- Graph/Tree Diameters
- Binary Lifting
- Tree Flattening with Euler tours

Also, to make sure you have actually learned that stuff, I made a custom Gym set on CodeForces that will last two weeks that hopefully is really good practice for making sure you have learned this stuff. Here is a link to the gym set; it will be available 45 minutes after the video comes out so that people have time to watch the video before starting the set, if they are interested in penalty points. All of the problems in the gym are original to this set (in their flavortext at least, some are simple enough that I'm sure they have appeared in other contests before).

The new gym integration was heavily inspired by Errichto's Matrix Expo set format. Let me know whether it's helpful. I think it might be, but also it's a pretty big time commitment to make it, so whether I keep doing them depends on how helpful they are to people.

If you have any questions or suggestions, feel free to leave them below. I hope you enjoy the problem statements, and I'll leave you all with this:

## Solutions

**Update:**

Really appreciate your effort for helping beginners to learn new concepts and improve!

Looking forward to it!

SecondThread orz

Hey, I understand that you want to make a point, but don't you think tagging them is unnecessary? I believe noone likes to be tagged just to see random people being orzed.

Guys, please listen to Ari, don't tag anyone unnecessarily.

Stop tagging Ari, She doesn't like it

Hey, I understand that you guys want to make a point, but don't you think tagging Ari is unnecessary? I believe no one likes to be tagged just to see themselves being joked about by random people.

I fully support condemnation of this practice. Looking at a notification just to see myself being tagged unnecessarily by random people is sure to be tiring.

In this case, however, I do not think it is a harm. It is a blog written by the "orzed" person, and he (assumingly) reads and enjoys comments here.

No, no, why are you saying this? I'm pretty sure that neugis tagged me for a really good reason.

Or SecondThreaded

For the record, I like being pinged so that I know when people are talking about me. At least, I certainly don't mind.

SecondThread We have company! Amazing videos, please continue.

Wait..... How come this is on the frontpage of CF?

Wait, I have no idea...

Looks like SecondThread's contribution will become highest on Codeforces shortly :)

probably a combination of post time and up-votes, plus SecondThread is getting more recognition in this community for good contents :)

Errichto and Thread are changing the learning game istg.

At this point, since everything's already prepared for it, they might as well add your stuff to EDU

## Spreading CP knowledge!

What is the problem with Codeforces EDU? I enjoyed all of their lessons so far. They are all doing a great job. No need to compare 3 great resources.

I'm not criticizing EDU or saying it's inferior to others.

I'm talking about the

quantityof videos/resources or more like time taken to make new educational videos.Fortunately, it's not a competition. It's more like we push one racing car together (because the community gets the sum of all resources).

Also, there are people like Mike or awoo who actually do significantly more, it's just not in form of videos.

Ari just told you not to do that.

LOL

Yeah, listen to Ari, no unnecessary tags.

Don't spam.

Well said.

Thanks, we will try our best to learn new and interesting concepts.

Loved the new format ! This helps gain much more confidence in the covered than just watching a tutorial on some topic and not getting custom problems on that topic.

Thanks, that’s exactly what I need.

The gym problems are very nice for reaffirming one's understanding of tree basics!

For problem D, what was the offline solution you had in mind? The text at the top implies that there exists an offline solution that's simpler than the online solution, but the only thing I could think of is some wack dfs + segtree solution where you update the answer for a query whenever you reach one of its endpoints. That seems way too complicated to warrant explicitly disallowing offline solutions lol.

Yeah it's definitely harder to do offline than online. I was also considering at a small-to-big + dfs with a treeset/segtree which I'm pretty sure is possible and can be done in n*log^2(n) without

toomuch difficulty. The main thing is that this might show up as a sub-problem that you have to answer online, so it's good to be able to do that, rather than finding some complex way that ACs but doesn't actually use the new topics at all.I really love your content

Your screencast(s) are really helpful.

Please give any hints for second problem.

https://www.geeksforgeeks.org/find-farthest-node-from-each-node-in-tree/

By the way, can you help me out with my implementation? It's giving WA on test 4..

This might help you.

ty, forgot to update max with the old diameter..

Dear SecondThread,

Thank you for all the hard work you put in daily! Know that it is recognized and greatly appreciated, It’s an honor for our codeforces community to have someone like you.

May you reach every height of success!

Hey nice video,

In the tree shown at 36:11, how can I find the sum over the path starting at the node with input time 3 and the node with input time 13? I can think of a way to do it but it requires calculating LCA of the endpoints, is there a way to do it without LCA?

Thanks! Correct, if you need to find some path aggregate in which one node isn’t an ancestor of another, you need to split it into two paths using LCA. (Unless there is some other more clever way that I’m not aware of)

Can Someone Explain the Samples of Problem F Please

Yeah, maybe part of it isn't super clear: for each query where x = 0, you can consider pairing things however you want just before for that query. These pairings aren't permanent, they are just considerations for that query. The way of making the pairings is to do minimize the sum of distances between every pair of paired seeds/pots.

expecting more and more from all of you

thanks SecondThread

The gym-set along with the usual videos format is indeed really nice. Thank you for your hard work.

I've just failed my math exam because of secondthread, he says that pi = 3 in 102694A - Circumference of a Tree, so I used pi = 3 to calculate the volume of a sphere and I got 0 marks on that problem. Guys, don't trust secondthread.

Clearly the teacher's solution is wrong then. I have an elegant proof that pi is equal to 3, but it's too long to fit in the margin of this comment thread.

For people not getting this,its a reference to Fermats theorem

A bit off topic, but can we expect a video on round 1 problems of hackercup, SecondThread ?

He said he won't be taking part in hacker cup in one of his streams.

I think...he said something along the lines that he wasn't sure if would be allowed to....but I might be wrong...

Also, I found a comment on youtube...where he answered it so I guess that's it for this thread! (which is a cool line that I am hoping will catch on...lol)

I’m not allowed to compete in hackercup, so probably not from me. I know Errichto has videos of him solving them, and there are some nice solutions on the Hacker cup website, too.

Why aren't you allowed to compete in hackercup?

He works at Facebook. Employees are not able to compete, similar to the policy for GCJ

Hey guys, I am bit stuck on prblem B, my approach was:

If distance of some node from 'root' is same as diameter or zero, the answer for this will be diameter+1 , else the answer for this node will be same as diameterI am getting a wrong answer on test 6. Can someone plz help?

Any general tips to debug will also be appreciated.

There can be multiple "root". Take care of that also

I took care for that. I tried DFS from any general node..got all the max distant nodes.. Then ran a dfs from all of them to get their end points...of maxDiam..

Then simply checked if for i is it in the stored points..I added diam+1 as asnwer..else diam is answer.

Getting TLE for TC4 https://mirror.codeforces.com/gym/102694/submission/90401537

Make sure you dont linearly check, probably some faster way

Spoilerbinary search

I didn't get the spoiler hint..It would be great if you could eleborate with some another example which could relate things up.

As I got that searching linearningly for N nodes would difinately be TLE...

Find the boundary points. Think of a better way than a linear search.

SpoilerSort the boundary points and binary search

There is little confusion. I am using Set for finding the elements if the element is end point of diameter. The only linear part I am doing is with find function which calculates the dep and diam initially.

Yeah, that's not the intended solution at all. There shouldn't be any need for a log factor here.

SpoilerYou know that the farthest node from any node on a tree is an endpoint of a diameter (as described in the video), right? It's also true that for any diameter and any node x, one of the endpoints of that diameter is the farthest node [technically at least ties to be farthest] in the tree from x.

So you should only need to check the distance between the node you added and two other nodes in the tree. After some linear precomp, you should be able to answer this in O(1).

What if the tree is like this?

TreeFrom node 1 you will find 7 (or 8) as the farthest node. And then from 7 (or 8) you will find 1 as the only farthest node. The problem is that your algorithm will consider only 1 and 7 (or 1 and 8) as all the possible nodes which should increase the diameter by 1, but actually all of them will increase it (1, 7, 8). You can try this example yourself for more details.

Hey Brodicico! Could you(or anyone) please tell me what mistake I am making. (I am continuously getting WA on test 5 and I am unable to figure out what mistake I am making — whether it is the implementation or the logic)

LOGIC :Find the diameter of the tree. For all nodes if its degree is one(or zero) then output (diameter + 1) else output (diameter)

CODE :A counter test case with its correct output is highly appreciated.

Try a case where you have a tree in a chain form like 1-2-3-4-5-6-7-8-9. Then connect node 10 with vertex 5.

The tree will be :

For node 10 it isn't correct to output diam+1.

bro i am getting correct answer for this one but still my code fails no 4th test case

Send me your code in private.

There's a hint in the video..watch it again....

This was a wonderful effort . can anyone give me the link for other episodes.

Here's the playlist.

Your screencast are really helpful. Thank You!

1006E - Military Problem

Great problem on one of the concepts explained in the video. Highly recommend it!

My solution for reference: 90223865

Someone give me a hint on E, please

I tried to find the topological order and size of each subtree. Then did used segment tree for updating and range queries. But it fails in test 4. Any idea guys?

Overflow issues .. Even i am not getting how to do that .

Maybe we should divide all node values by 10^6 and then build ST on doubles? If product on subttee is bigger then 1000, print 100000000, else print result

SpoilerLogarithms

thank you so much!!! it worked!!! you are amazing

How did you deal with log and pow function precision issues??

There were no issues for me with pow and log2. Code

As a beginner, your episode has really taught me a lot about the many application of trees. Thanks a ton for this!

Hey can someone recommend a good list of problems which contains different( hopefully all theory) types of problems on graphs and/or trees....It would be much appreciated....

can someone help me , I am getting memory limit exceeded in B. https://paste.ubuntu.com/p/6B8kdrvkyh/

Hey buddy, I saw your code, actually I think you can simplify the structure itself. Also the variable parent can be avoided, vis can be made boolean. Have you checked if the bfs doesn't explodes the queue?

I made vis boolean , now i getting tle. thanks man ,Changing the structure now. changed endl to "\n" got accepted.

Didn't know "\n" could change a whole lot of things. Followed some blogs on CF on this. Finally got AC after all the TLEs.

The thing is that endl flush the output and that is slow...

SecondThread

Any idea for D. Till now , this is how I am going: ( I guess m is always n-1).

Am I going the right way ? ( Wrong answer on 6)

I do actually have an idea of how to solve D, yeah. Your general approach seems right, but I’m not going to discuss solution details too in depth for a week or so.

Someone give me a hint on F, please

My 2-dfs calls in B is receiving TLE, is it expected ?

Nope! Use "\n" instead of endl, if you're using endl.

I am not using "\n"

https://mirror.codeforces.com/blog/entry/63071#:~:text=Sometimes%20using%20endl%20in%20place,and%20flushes%20the%20output%20stream.

https://mirror.codeforces.com/blog/entry/43780

I faced many TLEs yesterday due to endl. Followed these blogs mentioned above then got the ans.

This is not the reason for my TLE verdict

I did 2 BFS calls too and it's getting TLE for test case 16. Can't seem to figure out why its TLE tho when at max the complexity would be O(2*(6*10^5)) which is around 10^6. Can anyone help?

figured it out now, indeed changing endl to "\n" helped. Thanks!

My code for problem B https://ideone.com/dd5gOx with poor English commentary :)

Math burned me, yet again!!

SecondThread

For E the products can be as large as 10^500000 ( that is ((10^5)^(10^5)) ) and we have to store approx 10^6 of those.....how can it be done....can I get a hint?

you can use logarithm to store the values, and then restore the value with a power of 2

Wouldn't we loose precision? Can you plz elaborate...?

The problems are really, really good. Even if they use basic concepts they were not actually so easy (at least for me). I found the fifth problem very cool and challenging if you don't have experience using mathematical functions (I didn't until this problem :D). I give all my respects to the creator of this problemset. Thanks for investing your time in helping others.

Thanks, I’m glad you enjoyed them :)

For question C the code is running on my computer btt it shows runtime error for test case 3

90309168

since contest is running, nobody can open the link you posted. Also since test case 3 is visible you can directly debug in local environment to find reason for RE.just use gdb/lldb to step through code.

test case 3 is running in my local env just fine. btt it shows runtime error whwn i submit.

I haven't registered yet does that mean i'll have to wait for next 12 days?

You can register by going to the gym tab.

Image:Hello, i am currently at problem E and i dont understand how does it is not 1.5 in the second example, at the second line.

Since it is 1-rooted, 1 has value 3, 2 has value 2 and 3 has value 1 , no ??? what did i missed here ??

In the second sample, none of the nodes are ever updated, so all nodes have a value of 1. (Since the product of 1*1*1... is 1)

Could anyone please help me with problem E?How to handle such big numbers?

this is answered above :

SpoilerLogarithms

I realy enjoy E, although I've made 17 WA.

Can someone tell me why bfs doesn't work in promlem A to calculate the diameter. It is giving me tle.

It worked for me, maybe because you're using python and the time is a little tight, try reading this blog to speed up the input and the output. If it doesnt work to you, maybe trying with c++ will be a better way, because I see that the few persons that got AC, their execution time is around to 900ms

Hi, this is my first time solving Tree based problems on CF. I am having trouble understanding how to give the edges so that I can construct a tree. For example in the first question, the test case is

3

3 2

2 1

How will we construct a tree with 3,2 as edges and which node will they belong to? How will we construct a tree with this? I have seen some other solutions using arrays but i can't understand them.

Leetcode does it like this-

I have previously solved on LeetCode where we had to just write the function so I am very new to this.

The one you are constructing in LeetCode is Binary Tree having almost 2 children.

Here you need to use Adjacency list, which can be implemented via vectors in C++. You should read about it.

Any hint for problem F? (getting wrong answer)

SpoilerFix some root. Assign +x to seed and -x to pot. The edge a — b will only be used if there is net positive/negative sum in the subtree of a or b (whichever on more depth).

Thank you very much for this effort. I really liked the problems.

I solved F by observing some examples, can somebody give proof for F, why this works?

if you supposed a minimal matching where there are only two nodes on each side , and you paired each node with the one in opposite side you'll find that you can minimize more the distances by pairing each node with the one on it's side.

I am looking for why this is always true. Maybe here it is easy to observe.

Consider the graph: 1-2, 2-3, 2-4

Node 1 and node 3 have one seed each. Let us consider that there is a pot in the subtree of node 4 (let's call this node A) and a pot in the rest part of the tree above of the node 1 (let's call this node B).

Optimal solution is to match seed-3 with pot-A and seed-1 with pot-B, because the sum of distances is equal to dist(3, 2) + dist(2, A) + dist(1, B). If you try to "cross" the edge (1-2) and match the seeds in a different way, then the sum would be equal to dist(3, 2) + dist(2, 1) + dist(1, B) + dist(1, 2) + dist(2, A) which is larger than before by 2 * dist(1, 2).

As you can see, you can never avoid some distances, so there is no reason to try to match a seed with an other pot when there is already one available in its subtree.

Thanks, That makes sense.

You are welcome !

I'm failing to understand the input test cases in problem F, where queries are :

Above, 2 seeds at node 1 and 2 pots at node 6 make 2 valid pairs(path 2->6 goes through edge 3-4). Similarly, 3 seeds at node 2 and 3 pots at 5 make 3 valid pairs. This path goes through edge 3-4. This makes a total of 5 valid pairs.

Similarly, 2 seeds at node 6 and 2 pots at node 2 make 2 valid pairs and this path connecting 2 and 6 also cross the edge 4-3. So, total of 5+2=7 valid pairs. But, why is the answer for that query 3?

I'm missing something here, kindly help!!

Also, it's written

`The total sum of the distances between every pair must be minimized to keep clean the air.`

Does this mean something? I didn't get it.It's not required that seeds should placed in the respective

same potswhich were created while creating seeds in each query.In the above example for the last query we can use 2 pots at node 2 for placing seeds from node 1 or 2.

This means that sum distance travelled by

eachseed from the source node to node which contains pot should be minimized.Will all the test cases be made public after the contest ends?

Sure, I can do that. Ping me after if I forget :)

Thanks for the support!

ping

Thanks for the cool problems!

I couldn't figure out D. So I'm waiting to check other solutions for some insight.

please make the test cases public.SecondThread

How do I do that?

SecondThread, Can you make the test cases public now?

I am getting TLE in TC 4. I stretched the tree along the diameter and after that if node is added to the end points of diameter then dynamic diameter will increase by 1 otherwise not. My solution is here. Please somebody tell me error in my approach.

Your solution can't be opened until the contest ends. But the solution for your problem is answered above. Link to comment

1 sec time limit is pretty rough for java?

For which problem? I think all the judge solutions are in Java, but I can bump it up if it’s too tight.

For B, i feel like my sol would pass with 2 sec limit, but if not that is fine, I can move on

global_optimum you now have 3x the time limit (3s). One might say it's now a big_boy_time_limit.

Can someone please explain me what's wrong in my logic for B ? I seem to be repeatedly failing test case #6.

LogicFind the diameter of the tree and one possible endpoint of it, as mentioned in the video. Then, for each node just check if it's distance from the start node (which is the endpoint/start point of some diameter of the tree) in 2nd BFS (referencing from the tutorial video) equals the diameter; if it does the answer is diameter + 1, else it is the diameter itself.

This will not give you the correct answer.

SpoilerLet's say you have the following tree:

Your approach would not give diameter + 1 for all the relevant nodes i.e. 1, 6 and 7

Thanks a lot, that helped!

Can anyone pls provide some test case for problem C? im getting WA on test 4

I am also stuck on test case 4 with problem C

SecondThread any hints?

I'm not sure if it helps, but I recommend you take a look at the cp-algorithms.com lca tutorial if you're stuck.

silverfish No I know about lca and other algorithm to solve it. It is just there must be a corner case that I am missing. Hence wanted some hints

Kush.code Can you link your code?

Most likely your logic for calculating c'th node on the path from a to b is wrong. Key is to use LCA to determine whether c'th node is from path (A -> LCA) or (LCA -> B).

sjay05 https://ideone.com/A9iHTM I tried but I could not find any issues. can u take a look

I solved the problem from a bit by the same idea as you. but i was getting Wr4 because the function that lift the node up by c. so, i advise you to check it, this is my submission, i hope it helps you. - wrong submission - Ac submission

Draw a tree and make all possible cases for k, that's all you need to do.

FinalBoss_ there is no k in problem c

In question no C these are the arrays in my programm my code is runnig is fine in my system btt shws runtime error when i submit can this be a memory issue??

Inside main

SecondThread, what's the constraint of q in D?

Oh, good question. I’m pretty sure it’s 3*10^5. But like, answer each query in sublinear time please and you should be good to go.

Thank you very much for the problemset, it's very helpful! I like how it's not just straightforward "implement LCA and be done with it", but includes some modification that forces you to understand the concept behind it.

I have a question about F. It seems that the observation is

SpoilerFor a subtree rooted at node $$$v$$$, if there are $$$x$$$ seeds and $$$y$$$ pots in a subtree, it's optimal to match $$$min(x, y)$$$ within the subtree, and the rest will have to go through the edge between $$$v$$$ and its direct ancestor. This reduces the problem to simple subtree sum.

Which seems intuitively true, but how to prove it?

SpoilerAssume it's not true (i.e. it's better to match pots/seeds from different subtrees):

Now if you try to match the pots from subtree rooted at $$$v$$$ with the seeds from subtree rooted at $$$u$$$ your distance would be the distance from the pot nodes to $$$v$$$ + distance from the seed nodes to $$$u$$$ + $$$x$$$ (the number of matched pots/seeds) times the distance from $$$u$$$ to $$$v$$$;

Note that you would also need to match the seeds from subtree rooted at $$$v$$$ with the pots from other subtree (let's say $$$u$$$ in this case) and by the same account it would result in the distance from the seed nodes to $$$v$$$ + distance from the pot nodes to $$$u$$$ + $$$x$$$ (the number of matched pots/seeds) times the distance from $$$u$$$ to $$$v$$$.

This would give us, in total, at least $$$X$$$ (total number of matched seeds/pots) times more the distance from $$$u$$$ to $$$v$$$ by matching seeds/pots from different substrees than if we were matching in the same subtree.

Can i get a hint for problem F.

The problem is strictly linked with the idea of minimizing the sum of distances between seeds and pots, thus the first thing to think is "when is it necessary to cross edge $$$E$$$ when matching a seed with a pot?".

This is not likely a hint, but let's try to see something intuitively in the following cases:

Here you have a pot and a seed in the leaf nodes, thus the result in any edge would be 1 since there's no other matching possible/closer/minimum. This would give us a total sum of 2.

Here our options would be:

Note that the second option is better because we don't need to cross the root edges multiple (extra) times. Thus, matching seeds/pots locally (closer matches) might reduce the total sum. Also, looking at the last example, try to answer the question "when is it necessary to cross edge $$$E$$$ when matching seeds and pots?" thinking locally.

After solving (or if you still can't solve) the problem look at the spoilers on this thread to clear any doubt.

Thanks bro for your effort, I think i got the key now.

My IdeaLet's fix the tree to be rooted at node 1, then for each query with type(1) add +X on seed node and -X for pot node. Now, we need to pass through the edge from a — b, iff the total absolute sum of seeds and pots in subtree of node with max depth between a, b is greater than zero and this will be the number of time to pass through this edge also.

I am not able to solve circumference of tree problem .. also there is no editorial available .. what to do ?

Watch SecondThread Trees video ?

It's really helpful! Thank you !

Can some one plz provide a link to their submission for E. I have understood the approach (partially), but having problem to implement it.

https://mirror.codeforces.com/blog/entry/81527?#comment-683124

Why are you directing me to my own comment?

Click on the link and see the topmost comment on the screen. Its tumaryui's comment.

Great video, thank you!

Also, I think a thing worth mentioning is that segment trees can be directly utilized on incomplete rooted

binarytrees, while this tree -> Euler path -> segment tree approach helps us reduce the complexity when trees aren't necessarily binary.In problem E SecondThread, I got 4 WA's using Fenwick/BIT Tree but as soon as I changed it to Segment Tress, it got accepted.

Solution using BIT/Fenwick Tree

Solution using Segment Tree

I am unable to understand why it is happening. If anyone has solved this question using fenwick tree, please look at the code and tell where is it going wrong or share your code.

UPD:I got the error. I was constructing the fenwick tree in a wrong manner.Hmm, my model solution uses a fenwick tree. That’s odd for sure...

If that's so, then I must be doing some silly/ gross mistake that I am unable to find. I hope you wouldn't mind to taking a peek at my code unless you're busy.

Anyway, thanks for the clarification.

UPD:I got the error, I was construting the Fenwick Tree in a wrong way.Nice guide, really appreciated.

For everyone looking for other problems to apply the tree flattening trick, here are some from CSES problem set:

Can someone please let me know why My code fails on test 5(Problem B)

code

For problem C: Assume tree like following.

Is the answer (2, 1) or (2, 3)?

2, 3

Can you hint me, what is the wrong here (if you solved the problem). My solution

`UPD: SOLVED!`

Can anyone tell me why I am getting the wrong answer for testcase-4 in problem E, or give me any test case? SecondThread

code

It is due to overflow issue. You can see this thread for fixing it

Can i get hint for problem F.

Being discussed here.

Can someone please explain me the way to approach problem F.I have been stuck at it for quite some time now !!

.

can anyone tell how to do c

Auto comment: topic has been updated by SecondThread (previous revision, new revision, compare).Really enjoyed thanks.

SecondThreads java solutions were posted, but if you need A-E in c++: A B C D E F

I'm facing a weird error. The same solution is getting accepted in C++14 and getting RE in C++17 for Problem B.

Accepted codeSame code is getting RE in C++17, I tried

1. using memset for dist arrays

2.return 0 for main fn

SecondThread What could be the problem?

That sounds more like a C++ issue to me than anything else. If I had to guess, I would say it's likely a stack-space configuration issue. I think the RTE is probably that your recursion goes too deep on the rope case for instance, which causes you to run out of stack space.

I don't know what differences there would be otherwise, although I was under the impression that your main was always suppose to return 0...

SecondThread It throws RTE even for n=1, So I don't think it's a stack space issue. Ya main always return 0.

Maybe any C++ experts could help us Errichto

Guess who was right: https://mirror.codeforces.com/blog/entry/81527?#comment-682038

For n=1

`farthestnodefrom1`

will not be initialized before the second dfs, which is the cause of RE I think (undefined behavior)SecondThread the problems aren't available in the gym contest

What do you mean? The problems look available to me...

SecondThread if possible, maybe u can link the video editorial as editorial in the problemset. That may help a little bit

249461942 can someone please help for 102694B - Dynamic Diameter why is this failing for test case 6?