pure's blog

By pure, history, 7 months ago, In English

First of all I am new to trees and graphs so sorry in advance if I make any silly mistakes.

I was trying to do this question greedily as follows: I first initialized a deque res and set of integers inserted which says that certain input is already processed. for input u, v, x, y if u does not exist in inserted if x > y then I push u to front of res else to the back if v does not exist in inserted if x > y then I push v to back of res else to the front and then using res I created the vector finalres such that finalres[res[0] — 1] = n, finalres[res[1] — 1] = n — 1, ...

This approach does obivously seem wrong (eg, 1 4 1 2 2 1 3 4 2 1 1 3 2 1 ) which will give 3 2 4 1 which is wrong as 3 < 4

The code was accepted to my shock but not only that the code which I thought to be correct was not accepted. Link for accepted code: https://mirror.codeforces.com/contest/2143/submission/339152761 Link for the code which I thought should be accepted: https://mirror.codeforces.com/contest/2143/submission/339142596 The reason I think the second code should be accepted is because it passes through all the edges of a certain number before going ahead so the above issue would not be caused.

I would be grateful if anyone could help me understand what is going on.

  • Vote: I like it
  • +2
  • Vote: I do not like it

»
7 months ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

you are essentially doing something similar to toposort (a linear ordering of vertices in a directed acyclic graph (DAG) (a graph with no cycles) such that for every directed edge from vertex 'u' to vertex 'v', 'u' always appears before 'v' in the ordering) ... check this ... I exactly did that (while in contest I spend lot of time coding D1 and thinking about D2 optimisation so missed it) ... You are assuring that the vertex that comes before (in the front) gets higher value such that optimal ans (sum of max(x, y)) is achieved and that's what is asked!