Sorry for such difficulty balance. Here is the editorial.

Tutorial is loading...

**Behind story of B: Original B was harder. None of 2100+ rated testers solved original B, so it got downgraded. Also there was more than 15 pretests before.**

Tutorial is loading...

Solution Code for B**Behind story of C: C is created before few days to contest. If there was no current C, the contest would have hell balances.**

Tutorial is loading...

Solution Code for C**Behind story of D: Honestly I predicted D as hell hard problem. But other high rated people said it's not that hard.**

Tutorial is loading...

Solution Code for D**Behind story of E: I didn't expected such well-known problem. My solution for E is more complicated.**

Tutorial is loading...

Solution Code for E**Behind story of F: This problem was located at D originally.**

Tutorial is loading...

Solution Code for F
what a cute maths in e and f. It looks like i will learn whole maths from codeforces itself and even surpass rng_58 .. LOL.

D is a nice problem .

Wow very fast editorial :D

Woah. That was fast.

C made me sad :[

Why my solution for C (id:55462095) wasn't judged when I submit at 01:59:45?

It says wrong answer on pretest 1

McDic "Behind story of E: I didn't expected such well-known problem. My solution for E is more complicated."

what u mean by it. as u were the setter of this problem and u r urself saying i didn't expected?

My intended solution was much harder than tester's solutions

ok.

What was your intended solution?

Please read editorial.

Great problems. Fast editorial. Please set more rounds McDic.

Also, what does balance mean?

Difficulty balance. See number of solvers for each problem.

Anybody can tell me please, if my code for E is wrong in idea or there is a bug??

codeAll ready functions are copied from the net or from old codes. Where "solve" function gives the discrete log. full code

Wow a fast editorial

arsijo pls stop creating problems (c)

https://www.youtube.com/watch?v=KNviwfDeRKg&feature=youtu.be

He-he, nice one :)

https://youtu.be/e-ObfcCJsBs

Is test case weak or this is correct?

gamegame said this is so slow for some cases. I'm sorry about that. (I put some of countercases that some solutions work too slow for it)

It will get TLE on following case:

I feel shame that I using this method to pass problem F in the contest (>__<)

Do other people also feel that this contest wasn't balanced.

Nope

Alternate (easier) solution for E:$$$5$$$ is a primitive root modulo $$$10^9+7$$$. So, for every number $$$x$$$ in $$$(0, 10^9+7)$$$ there exists $$$p$$$ such that $$$5^p\equiv x \pmod {10^9+7}$$$. This is known as Discrete Logarithm and can be calculated quickly with Shanks algorithm.Now, lets take discrete $$$\log_5$$$ in both sides of the recurrence:

$$$\log_5(f_n) = (2n-6)\log_5(c) + \log_5(f_{n - 1}) + \log_5(f_{n - 2}) + \log_5(f_{n - 3})$$$

If we let $$$F_n = \log_5(f_n)$$$, $$$C = \log_5(c)$$$, and $$$g_n = 2n - 6$$$, then it transforms into:

$$$F_n = g_n C + F_{n - 1} + F_{n - 2} + F_{n - 3}$$$

We can calculate $$$F_n$$$ directly via matrix exponentiation and then calculate $$$f_{n} = 5^{F_{n}}$$$.

This one is much more straightforward and natural in some sense.

Even simpler solution: $$$f_n = c^\alpha f_3^\beta f_2^\gamma f_1^\delta$$$ for some exponents $$$\alpha$$$, $$$\beta$$$, $$$\gamma$$$ and $$$\delta$$$, which according to Fermat's little theorem need to be known modulo $$$P-1$$$ for $$$P=10^9+7$$$. Thus first calculate exponents using fast matrix exponentiation modulo $$$P-1$$$, and then calculate powers using fast exponentiation modulo $$$P$$$.

how to calculate the exponent of C using dp recurence? that was the main problem, the rest are easy.

You have recurrence $$$\alpha_n = \alpha_{n-1} + \alpha_{n-2} + \alpha_{n-3} + 2n-6$$$ which can be rewritten as $$$\alpha_n = \alpha_{n-1} + \alpha_{n-2} + \alpha_{n-3} + 2\varepsilon_{n-1} - 6$$$ and $$$\varepsilon_n = \varepsilon_{n-1} + 1$$$. Then use matrix of size $$$5\times 5$$$ to calculate vector $$$(\alpha_n, \alpha_{n-1}, \alpha_{n-2}, \varepsilon_n, 1)$$$.

OMG thank you very much. I just got stuck at the power of C, now i get it thanks.

You still able to make this easier:

and

If we compute the difference:

and we get the following easy recurrence:

What to do after building recurrence

Compute the values for the value of n that you need. You can solve this in O(log n), search about computing values of linear recurrences using matrix exponentiation.

but how to form transformation matrix. any pattern to follow for it.

Search for Linear Recurrences section in this book

For $$$b_n = \alpha_n + n$$$ recurrence takes the same form as for $$$f_1,f_2,f_3$$$.

Since, $$$2n-6 = 0, 0, 0, 2, 4, 6, 8, 10, 12, ......$$$ then, $$$α_n=α_{n−1}+α_{n−2}+α_{n−3}+ε_n$$$ and $$$ε_n=ε_{n−1}+2$$$, where $$$ε_1=ε_2=ε_3=0$$$ and $$$α_1=α_2=α_3=0$$$

Update:I got AC with this idea.Can you explain your matrix please? $$$a_{n - 2}$$$ = 0 * $$$a_n$$$ + 0 * $$$a_{n - 1}$$$ + 1 * $$$a_{n-2}$$$ + 0 * $$$g_n$$$ + ???(5th)

I wonder what is the fifth elements in this matrix ?

Edit: Oh,nevermind,just silly me.

\alpha can be translated to, say

we define theta_n = alpha_n + n we have theta_n = theta_n-1 + theta_n-2 + theta_n-3

cool solution, we don't need to worry about finding primitive roots .

Learnt a lot from your solution.

Can you explain why it is P-1?

Because of Eulers Theorem. https://en.wikipedia.org/wiki/Euler%27s_theorem

How do you calculate $$$\beta, \gamma, \delta$$$? My original thought was that $$$\beta_n = \beta_{n - 1} + \beta_{n - 2} + \beta_{n - 3}$$$, but this would give the same recurrence for $$$\beta, \gamma, \delta$$$ (and hence they'd all have the same result).

Yes, recurrence is the same, but initial conditions are different: $$$\beta_0 = 0, \beta_1 = 0, \beta_2 = 1$$$, but $$$\gamma_0 = 0, \gamma_1 = 1, \gamma_2 = 0$$$, and $$$\delta_0 = 1, \delta_1 = 0, \delta_2 = 0$$$.

I'm trying to follow along with this approach but my math is not working out, could you help me find where I'm going wrong?

Consider the case:

n = 4, f1 = 1, f2 = 2, f3 = 5, c = 3, and modulo 1e9+7

log5(f1) = 0

log5(f2) = 381838282

log5(f3) = 1

log5(c) = 884237698

We get for log5(fn) = (2*884237698+1+381838282+0)%mod = 150313665

5^150313665 % mod = 200000005 , which isn't the answer (should be 90).

What did I do wrong?

In the end you are doing $$$f_n \equiv 5^{F_n} \pmod{10^9 + 7}$$$. For that to be true, you should calculate $$$F_n$$$ in modulo $$$10^9 + 6$$$ (from Euler's Theorem).

Thank you.

How to calculate fn mod k.can u share Euler's formula

`How to calculate fn mod k`

: Explained above.`can u share Euler's formula`

: Here it is: $$$e^{ix} = \cos x + i \sin x$$$Can you explain why mod-1? (I watched Euler's theorem, but did not understand why mod-1)

By Euler's Theorem, if $$$p$$$ is prime then $$$a^{\phi{(p)}} \equiv a^{p - 1} \equiv 1 \pmod p$$$. Equivalently, $$$a^{k(p-1) + c} \equiv a^c \pmod p$$$. So, if you want to calculate $$$a^b$$$ in modulo $$$p$$$, then you need to calculate $$$b$$$ in modulo $$$p-1$$$.

Thanks)

Why can we calculate $$$F_n$$$ directly via matrix exponentiation? Isn't the $$$g_nC$$$ term still troublesome? Are you transforming $$$g_n$$$ into a recurrence like on monsoon's comment?

We can carry that $$$g_n$$$ term with the matrix. I think it is quite well known approach for doing matrix exponentiation simultaneously for 2 recurrences. For example if you have: $$$f_n = f_{n - 1} + g_{n - 1}$$$ and $$$g_n = f_{n - 1} + 2g_{n - 1}$$$, you can make something like this:

Got it, thanks!

I misunderstood the problem statement of B and thought that this example yields a YES :(

Same

There is no a ray of

from the center of your "+" to the non-empty space at the very bottom of your example.consecutivenon-empty cellsThus, the example violates the

All other cells are emptycondition.What was the harder (original) version of B?

The width of the vertical stripes and the length of the horizontal stipes are the same everywhere. For example:

..***..

..***..

.*****.

.*****.

..***..

YES

Can you explain more? I am getting WA at 15.

There is a mistake in your variable “NoIsland”. You don’t increase it sometimes. Test:

3 5

.*.*.

..***

...*.

Your output will be YES.

So, u can find the plus, then u must paint it. Now, u can check, that there is no unpainted *

Good catch. But why answer is YES in above example?

Your program’s output is “YES”. But real answer is “NO”.

Sorry I am talking about the star example you posted above.

Anand asked the original version of task. And this test was for the original version. (There is a simplified version in the round.)

Oh you mean for this problem answer should be "NO"?

Yes. Answer should be “NO”.

Thanks man. I solved it using coloring now.

Sorry for misleading you)

Also holds h,w<=500?

Yes

It seems not so hard...I think it can be solved with h,w<=2000.

Yes, it is real not so hard. But u need more time to solve it.

Check out Stars Drawing(Hard version).

Can you please give us your implementation of E, it will be mush easier to understand the solution this way.

I will provide. Please wait.

Am I the only one who solved D using hashes and DP on tree?

I used hashing as well, but no DP required.

Can you guys tell me what is hashing on trees or send any blog to understand Sharon lessmeaning

Hashing subtrees is similar to hashing strings. To hash a node I just sort the children by their hash, concatenate them in sorted order and concatenate number of children as well. That should represent a unique subtree.

Though in this problem I believe I forgot to sort the children, so its probably possible to break my code. Doesn't matter since I didn't solve it in contest anyway.

Thank you for illustration

Solution for E in log N * 5 ^ 3: Just count powers of f1, f2, f3 and c in which they appear in fn, so now our modulo will be 1e9 + 6.

How to calculate power of c?

Let Xi = numer of c's in Fi (Xi, Xi + 1, Xi + 2, C, 2) * M = (Xi + 1, Xi + 2, Xi + 3, D, 2) D = C + 2 and calculation of X is standard.

McDic maybe problem E is well known problem, because problem 1106F were before it. Why problem E is a 2250-point problem, if problem 1106F is 3000-point problem?

I predicted there would be not so big gap between D and E, that's why I gave E 2250.

Also I don't used discrete logarithm for my solution.

The first thing that popped up in my mind after reading E is 1106F - Lunar New Year and a Recursive Sequence too. Sad that I can't implement in time. Also, editorial solution for E is little tricky. I like it. Thanks for the problemset. :)

For question E, I don't use discrete logarithm or prime factor decomposition. But during the contest, I forgot to do modulo MOD-1 when calculating the power of matrix which is used for the exponent.

Lesson learnt now. I remember better little Fermat theorem from now on.

My solution: https://codeforces.com/problemset/submission/1182/55508849

Lesson from that: Upsolve problems as much as you can — you never know when a familiar problem would pop :)

In Problem B — why we should "search the end of the "+" shape from the center. (White stars in the picture below.)" Problem description says "There should be some (at least one) consecutive non-empty cells in each direction (left, right, up, down) from the center. In other words, there should be a ray in each direction." and it is enough to check that center has consecutive non-empty cells. And after we do not need to check this row and column (which contains our center "+"). My logic was : find "*", check if it has at least one consecutive "*", save row and column and then check other rows and columns for "*", if they contains "*" — then "NO", otherwise "YES". So what is wrong with this logic?

Because any “*” that isn’t part of the picture will make it a “NO”. There should be no “.” That breaks the continuation of any ray. Once there is a “.” At the end of a Ray, every other box should be empty, that is “.”

Yes, thanks, I understood.

There is another solution for problem D that finds the answer by rooting the tree at every node.

Suppose you have rooted the tree at node $$$1$$$. Now, for each node $$$u$$$, you have to find whether the subtree rooted at $$$u$$$ is valid or not (valid means if we ignore the rest of the tree, $$$u$$$ can be an answer). This can be done recursively. Let's define a string $$$s(u)$$$ for each $$$u$$$. If $$$u$$$ is leaf, $$$s(u)$$$ = "1" and $$$u$$$ is a valid node. Otherwise, for each child $$$v$$$ of $$$u$$$ if $$$s(v)$$$ is same, then $$$u$$$ is a valid node and $$$s(u) = s(v) + deg(u)$$$ for some $$$v$$$. For each valid node $$$u$$$, we calculate $$$s(u)$$$. We can't actually store the string in $$$s(u)$$$, right? Instead we will be storing the hash value of the string $$$s(u)$$$.

Now if node $$$1$$$ was valid, we can say it can be the root. If it is not, we will root at one of its child $$$v$$$. This way we will root the tree at every possible node. The only information we will be missing for a root $$$v$$$ will be the string for the parent $$$u$$$ (when $$$1$$$ was root) of $$$v$$$. We can calculate this information when going to $$$v$$$ from $$$u$$$ the same way we calculated $$$s(u)$$$. I have used a multiset for this. There might be a way to avoid the multiset, but I didn't feel the need to.

Code: 55462817

Complexity: $$$O(nlogn)$$$

It was also possible to cut through the tests of problem D like this: 55463619

Alternatively, for E. call $$$p = 10^9 + 7$$$, we need to find $$$f_n \mod p$$$.

Set $$$g_n = n + \log_c f_n$$$. We get the recurrence relation

By Matrix Exponentiation, compute $r_1, r_2, r_3 \mod p - 1$ such that $$$g_n = r_1 g_1 + r_2 g_2 + r_3 g_3$$$.

Now, our final answer $$$f_n$$$ will be

The time complexity will be $O\left(\log(n) + \log(r_1) + \log(r_2) + \log(r_3) + \log(r_1 + 2r_2 + 3r_3 - n)\right)$. Note that $$$r_i$$$ will be exponential in $$$n$$$ but since we are calculating the exponent modulo $$$p$$$ it is effectively $$$O(\log(n) + \log(p - 1))$$$ for all purposes.

Make sure to calculate $$$r_1, r_2, r_3$$$ modulo $$$p - 1$$$ and not modulo $$$p$$$ (because Fermat's Little Theorem). I made this exact mistake during the contest, cost me the entire problem and a possible top 50 rank FML. :(

Could you explain why it is P-1, also can you share any resource if possible? Thanks!

Please explain solution for problem D. For ex. 0 can be root. But we can't select root with degree 1 (for example 4) as root.

In this case 0 is the semitop and there is no top.

I see thx.

Same with this picture, but there is no vertex 1 and its subtree. 0 can be the root, but others can't.I think 0 is neither top vertex nor semi-top vertex.

Different way to think about D: suppose that $$$r$$$ is the selected root. For any edge $$$u \rightarrow v$$$ such that $$$u$$$ is closer to $$$r$$$ than $$$v$$$, the subtree below $$$u \rightarrow v$$$ can be described by a sequence of pairs [next branching factor > 1, distance to next branching factor > 1]. That's because anywhere the subtree branches, all children should be isomorphic.

The number of pairs in that representation is limited by $$$log(N)$$$. We can do a standard tree dp and compute the condensed representation (or detect that one doesn't exist) for each of the $$$2N - 2$$$ subtrees of the original tree. The final answer is any root such that each of its child subtrees have the same representation.

My implementation can be found here.

This is one of those solutions that seem so obvious when it's explained. And yet I spent an hour trying to get around the O(N) representation :p

Why do we have to iterate over prime divisors in E? Can't we just calculate the power of f1, f2 and f3 contained in fn the same way?

My solution to E:

We can get $$$\log_C F_n$$$ in $$$O(\log n)$$$ ,then easy to get the result.

Why so many downvotes?

can someone explain c more vividly? what they mean in "If you want to form a beautiful lyric with 4 words, then the lyric must be one of the things listed below;

Consist of two complete duos. Consist of one semicomplete duo and one complete duo."

if complete duo means same number of vowels + same last vowel

and semicomplete means same number of vowels only

Then yeah, it's just like you wrote

which is correct order of lyrics in C? give me 4 word(just complete duo semicomplete duo)

The order doesn't matter, just get as many lyrics as possible in output.

"the furthest directly reachable leaf node from semitop, and the closest directly reachable leaf node from semitop. It is guaranteed that the top node is one of those two leaves."

I don't understand why one of those have to be the top

McDic It can't be that all leaves are at the same distance from semitop? Or what am I missing?

Directly reachable nodes are the only nodes that you can find from only $$$degree = 2$$$ nodes. If there is only one node in inner tree, then all leaves including top node can be directly reachable leaves.

Thank you for an interesting contest! Can you please put some solutions in the editorial? The problems are well explained but it will be a good thing if you'll show the solutions.

Sure. Please wait.

I don't know why, but i misunderstood the condition of problem A at first :D. It took me a while to realise how simple it is. :))))

Actually, I misread problem C the first time, and I'm still wondering how to solve it.

I thought there could be more than two words in a line of a lyric.

Can anyone help me on this modified problem?

Do you need help on your "more than two words" problem , or the C problem ? I'm not even sure how the "more than two words" problem would work, the goal of C is to get as many lyrics as possible, and not as many same vowel words as possible.

The mote than two words problem is what I’m asking

i did the same thing and find i misunderstood until see this editorial. struggle for it the entire contest. since my misread solution can pass until pretest 16 that i didn't found i misread.

Could you give me some hint about the misread problem? I'm still wondering

it was a wrong solution. i just do same approach as editorial but after that two filter, there are some words left with same end of vowel can form pair of 3rd words. but it will broke when i have too many same vowel amount pair and have to break some of pair to form 3rd word pair.

Very cool problems! E is a nice problem, even I didn't manage to solve it.

C. Beautiful Lyrics: What about the O(N) implementation in the problem C? I did it with a array of maps :

`map<char,vector<string>>mp[1000002];`

so in the i'th position i have a map with all the words that contain i vowels. In that map the kth element (k is a vowel) is a vector with the strings that the last vowel is k. There is my solution:55461952So many approaches for D and E is very nice, it makes problem look natural rather than derived from technique.

I provided all solutions. Thank you.

It says you are not allowed to view the page when clicking on solution link

Hmmm.. Why is this happening O_o I will try to solve this issue

I used same approach for C, it's failing for testcase 7 and I'm unable to find the bug.

Code

Sorry for the messy code :/

UPD — Found the testcase.

Testcase 1Answer — 2

Testcase 2Answer — 1

Thanks to ajecc for this testcase

In D we may just find a centroid, and when find the path of two-degrees vertices, the end of this path need to be a root.

I also thought the same solution. Did you solve it by finding centroid? Does it work?

After finding centroid, how do you check whether the condition, nodes with same distance from the root will have same degree?

Yes, you may see

Why we use only sqrt(n) numbers in problem f?

Divide $$$[a, b]$$$ to $$$sqrt(n)$$$ blocks, and it's possible to search all area using only one block because of properties of modulo operation.

IN problem E, let power of c in f(n) be g(n). So, g(n) = 2 * n — 6 + g(n — 1) + g(n — 2) + g(n — 3). This could be done through matrix exponentiation.

Let matrix m be = {(1 , 1 , 1 , 2 , -4) , (1 , 0 , 0 , 0 , 0) , (0 , 1 , 0 , 0 , 0) , (0 , 0 , 0 , 1 , 1) , (0 , 0 , 0 , 0 , 1)}

Now {g(n) , g(n — 1) , g(n — 2) , n , 1} = m * {g(n — 1) , g(n — 2) , g(n — 3) , n — 1 , 1}

Similarly we can find powers of f1 , f2 and f3. Is E possible to solve in this manner?

P.S. :- I am asking this question because I am not able to solve this question.

P.S.2 :- Thanks. I solved the question.

Sadly, my solution for B didn't make it after the contest because I didn't check that with a test case like:

My function to find the center of the '+' would check for posible centers at i = 2, j = 1, thus checking in the array a position that didn't exist (i + 1, j).

It was easy to solve though, it was just changing

`if(i == x) return false;`

for`if(i == x - 1);`

but it's kinda sad, since I usually have a hard time with B problems and this was the first time I got one with pretests passed in a Div 2.Can anyone help me with this tle on problem C: https://codeforces.com/contest/1182/submission/55471836

D can be "solved" without thinking too much. While less than 0.7s has passed pick at random two vertices which have different degrees and are even distant apart, find midpoint of path between them, and call it $$$y$$$. Suppose we root the tree at $$$x$$$. Vertices in subtree containing vertex $$$a$$$ are closer to $$$a$$$, in vertex containing $$$b$$$ are closer to $$$b$$$, all other are equal distance from $$$a$$$ and $$$b$$$, so since $$$a$$$ and $$$b$$$ have different degrees cannot be our sought root. Those vertices form either a subtree or complement of subtree if we have vertex rooted at $$$1$$$, so we can mark all of them as impossible by just adding $$$\pm 1$$$ to one of those vertices or the root and then considering only vertices for which sum of values on the path to root is $$$0$$$ as valid. After that just check all such vertices with basic dfs. To do all this efficiently we need some basic implementional tricks. 55472896

I jumped up from rank 3500 to rank 2500 purely because of B hacks and system tests.

It says that i'm not allowed to view the requested page when i click to the solution. What is the problem?

If you need the code for a certain problem, just go to Standings and search for code that suits your needs, usually the high-ranked users have the same/better code as/than editorial's.

Yeah, you're right. I already analyzed some solutions on problem C. This problem is not so hard,but there is a lot of stuff to do and it gets a little tricky. That's why i was curious about the editorial.

Yes you just need to have a clear vision of what you're trying to do, that's why I always try to solve the problem on paper first because otherwise I get really confused at my own code if I try to think of the solution during the process of writing code, makes debugging a lot more painful and time-taking.

What got me about this task was my lack of knowledge about vector of vectors data structure which was needed for this task, otherwise this task was clean enough.

I found a similar solution to E, that worked when I upsolved the problem. Let $$$g_n$$$ denote $$$c^n f_n$$$. Then we have $$$g_n = g_{n-1} g_{n-2} g_{n-3}$$$, and thus for all $$$n, g_n = g_1^{x_n} g_2^{y_n} g_3^{z_n}$$$. Then the recurrence satisfied by $$$x_n, y_n, z_n$$$ is $$$x_n = x_{n-1} + x_{n-2} + x_{n-3}$$$, and same for $$$y, z$$$. Now use matrix exponentiation modulo $$$10^9+6$$$ to find the reduced exponents, and this enables us to find $$$g_n$$$ using the formula above. Then we find $$$c^{-n} g_n$$$ using inverse of $$$c^n$$$, and thus we're done.

How you find transformation matrix.

Consider the following three equations:

This gives the required matrix, upon comparing with the product of a matrix and a vector.

ok, so u have to use observations to find tranformation matrix.

Very nice and simple approach. I saw your implementation. Can you tell why you have used modp while taking modulo in multiply function instead of mod??

Can you explain why it is 10^9 + 6 and not 10^9 + 7? Also could you share any resource? Thanks!

reference: Fermat's little theorem

a^(p-1) = 1 (mod p) when p is a prime number.

McDic how you came up with the formula for g(x,p).

Can you explain why it will be sum of remaining three.

Because $$$a^p \times a^q = a^{p+q}$$$

Moved to a different post this

I think problem E can be done by matrix multiplication,and it is easy to think. Let $$$f_n=f_1^{a_{n-3}}\cdot f_2^{b_{n-3}}\cdot f_3^{c_{n-3}}\cdot c^{2d_{n-3}}$$$,then $$$a_1=1,a_2=1,a_3=2,a_n=a_{n-1}+a_{n-2}+a_{n-3}$$$,

$$$b_1=1,b_2=2,b_3=3,b_n=b_{n-1}+b_{n-2}+b_{n-3}$$$,

$$$c_1=1,c_2=2,c_3=4,c_n=c_{n-1}+c_{n-2}+c_{n-3}$$$.

$$$d_1=1,d_2=3,d_3=7,d_n=d_{n-1}+d_{n-2}+d_{n-3}+n$$$

And we can use Fermat's little theorem to modulo them with 1e9+6

why i am not able to see solution code. whenever i am clicking on solution page than it show you are not allowed to view requested page

I am also facing the same problem.

We could solve D by the following observation : if the answer exists and is not the center of the tree, then it must be one of the leaves.

Proof:Denote : dist(u,v) is distance from node u -> node v

Let two ends of the diameter of the tree is end1, end2, and the length of the diameter is d. Obviously end1, end2 are leaves.

Let k leaves in the tree be L1, L2,..., Lk. Suppose end1 = L1, end2 = L2.

Let the root we are looking for be x. If x is not one of the leaves then all dist(x, Li) must have the same value.

1, if d is odd, there is no such non-leave node u satisfies all dist(u, Li) is equal.

2, if d is even:

a, If k==2 then all L1, L2 and center is the answer, nothing else.

b, If k>=3 then if x is not the center nor leave, then there must be a leave Li (i>=3) such that the path x->Li does not intersect with the diameter. Because dist(x, Li) = dist(x, L1) = dist(x, L2). Then we could easily prove that one of two following inequalities must be true :

b.1) dist(Li, L1) > d.

b.2) dist(Li, L2) > d.

So in this case we find a path which length is larger than the diameter -> contradiction.

Therefore in two cases, if the answer exists then either the center or the leaves must be the answer (q.e.d)

Which nodes should we check ?

1, Both ends of the diameter, which is L1 and L2

2, The center

3, A leave Li such that dist(Li, L1) = dist(Li, L2). Here we can infer that center must exist in the paths from L1 -> Li and from L2 -> Li

3.1) if dist(Li, L1) < d. We will prove that if Li is not the answer, then the answer doesn't exist at all:

Proof: If Li is not the answer and Lj (j!= i) is the answer, and because dist(Li, L1) == dist(Li, L2) and dist(Lj, L1) == dist(Lj, L2), then: dist(Li, center) <= d/2 and dist(Lj, center) <= d/2.Because dist(Li, L1) < d ==> dist(Li, center) < dist(L1, center) = d/2.

==> dist(Lj, Li) <= dist(Lj, center) + dist(Li, center) < dist(Lj, center) + dist(center, L1) == dist(Lj, L1).

==> So Lj could not be the answer (contradiction).

3.2) If dist(Li, L1) == d then we can prove that if the center is not the answer, then the Li is also not the answer.

Proof : Suppose there exist u, v such that dist(u, center) == dist(v, center) and deg(u) != deg(v), then obviously dist(u, Li) == dist(v, Li) (which is easy to prove) and deg(u) != deg(v). So Li will not be the answer.

Overall, we have to check L1, L2, center, and at most ONE special leave Li such that dist(L1, Li) == dist(L2, Li) < d.

The complexity is O(n) or O(nlogn).

55464949

I love this type of problem, it's perfectly balance between mathematic thinking and coding.

3.2 is very wrong

Answer should be 9, not -1

Thanks for the idea.

3.2 must be fixed to the following :

If dist(Li, L1) == d, then when Li is the answer, for all Lj != Li, dist(Lj, Li) == d. So here just root the tree at the center, let the child of the center is V1, V2,...

We could easily see that if there are two leaves Li, Lj in the subtree of Vi then obviously dist(Li, Lj) < d, so both Li, Lj is not the answer.

So find the only leave Li inside the subtree of Vj, if the Li is not the answer, then the answer doesn't exist at all.

Auto comment: topic has been updated by McDic (previous revision, new revision, compare).Why binary search approach on prob C giving WA on test case 16? soln link -:https://codeforces.com/contest/1182/submission/55465546

can anyone tell me why my approach is wrong for Problem C

Aren't they different lyrics?

and

It's actually different beautiful lyrics. No matter which word you use, just maximize the concurrent number of beautiful lyrics.

They are different but you cannot reuse the words. So you can only use the word as many number of times as it is given.

For D

Can someone tell why this approach won't work.

We start with leaf nodes delete them, decreasing there parents degree by 1.Now we check if all parents have same degree(Note while checking we check according to degree of original tree) if this doesn't satisfy we return -1 else we continue this process.

"We start with leaf nodes" — I'm guessing you are considering all those nodes with degree 1 as leaf node. In the case, where the only possible root of the tree itself is having degree 1 (see the attached photo), you'll consider that node also as a leaf node, and go on traversing to its parent. So you'll have to handle that case as well.

In F how do we handle the case when q is larger than the interval size (i.e. sqrt(b-a+1)). Aren't we ignoring those values according to the tutorial.

A Simple python solution for Problem C. Hope it's useful!Submission link : 55487269

You can comment below if you have any doubt!.

Please, hide your code using the link.

How to solve 1182 B using dfs (just wondering as it is mentioned in the tags).

I also think but didn't, get it.... Plz explain

I guess you could use flood-fill to count no of islands and count how many islands are + shape. Like that.

please refer to this

debugged E 10 mins after the contest ended For slow typer like me, 2 hrs is not enough :(

Can anyone explain E to me please?

Lawali's solution. Find the diameter path and validate for two leaves of the diameter path. If no valid vertex found(i.e. top is not in the diameter path), then the semitop should be the middle of the diameter path. Now validate for the semitop and

any directly reachable leaffrom semitop. If any valid vertex found, print it. Otherwise print −1.I think you should check the nearest

directly reachable leafAh yeah, you are right. You are saying about example

1-2-3-4-5, 2-6-7-8

right?

yes, something like that

i hope that the solution will be fixed soon, it is not a big problem but can cause serious misunderstanding and many people may not look through the comments

btw,thanks for your nice problems!especially D, i like it a lot!

I fixed that part. It will be reflected to the post soon.

I used McDic's approach in problem C but not getting AC. Can anyone help (@McDic)

It's hard to read the code due to the indentations. Please make code pretty and re-ask.

Done

What is the verdict you got? WA? TLE? MLE?

I got WA for test case 12

For problem D, why the answer is only exist in the two leaves of the diameter path and the middle of the diameter path?

My code for problem B is giving run time error on test 16. Here is my code :- https://codeforces.com/contest/1182/submission/55501256 I am unable to figure out its cause ? Can someone help?

Can someone please explain how to find semitop in McDic's solution in the editorial? It says :"Then you can find the semitop by collapsing each level from leaf nodes of inner tree. " I keep collapsing each level untill what ? How do i know i have reached the semitop ? Because I can never know which leaf in the inner tree will end up on the semitop.

How to find two nodes of "directly reachable"??? And also how to find the furthest directly reachable leaf node from a semitop??

Can someone plz explain problem C, and its time complexity?

It, possibly, O(n), but it easier to implement it with O(n logn) complexity by using std::map.

How to calculate term by matrix exponentiation?

My approach for D: find the centroid of tree. If the tree is good, the centroid will lie on the path between top node and semi-top node. Then, I can travel to the topmost leaf node and check if this node satisfies or not.

Please, what are the math/algorithm prerequisites to understand the problem's E solution?

You might have to learn Matrix multiplication, Modular Matrix Exponentiation (which is very similar to Modular Exponentitaion) and Fermat's little theorem for this problem.

I have been trying to do B, and I think it also matches the editor's solutions, but idk, it is getting runtime errors saying that the index gets out of bounds. it kinda looks like this..

## include

## include <bits/stdc++.h>

using namespace std;

int main() { int w, h; cin >> h >> w; char arr[h][w]; for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { cin >> arr[i][j]; } } int f = 0; for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { if(arr[i][j] == '*' && arr[i][j + 1] == '*' && arr[i + 1][j] == '*' && arr[i — 1][j] == '*' && arr[i][j — 1] == '*') {

}

Does Someone have a simpler solution for problem C that does not use Hash-maps or RB trees?

here: 55450645

I sorted each word so that all the complete words would be adjacent and you can linearly scan the sorted vector later and find the semi-complete words

Can someone explain why my approach is giving TLE. Acc to me it should have o(nlogn) complexity.

My Approchstore features(no. if vowels and last vowel ) of each word in a map from string to pair<no of vowel,last vowel>

sort the input vector of string based on its features(first in increasing order of no of vowel and in case of tie in increasing order of last vowel)

now we can just iterate through the sorted array to form good lyrics

codemy codeEditorial for B is very very good. Thanks for the effort McDic

Can someone please explain how the time complexity of the editorial solution for C is O(nlogn) or O(n)?

I was attempting problem B and I got WA in test 9.

My approach is to first scan the whole picture in 3x3 squares for the "centre" pattern:

If the program detects more than 1 centre, it returns false. Otherwise, it records all the consecutive '*' grids from the centre in 4 directions into a list (with the centre included). Finally, it checks for any '*' grids in the picture but not on the list.

Can anyone point out the errors of my approach/implementation? Thank you very much!

Submission: 55542827