I have a few questions:
- When you saw this question, did you already know how to approach it?
- Did you look something up the internet which helped you solve it, if yes then how did you know what to search?
- If you knew how to solve it using common competitive programming Algorithms/Data Structure, then what did you use?
I think this may be helpful : https://codeforces.me/blog/entry/71545?#comment-597939
I haven't solve E, but here are my thoughts process if that helps.
Now, after reading the analysis, turns out it was indeed construction problem at the top level.
For me, I just tried to see what numbers I can put on the diagonal. From there (and sample 2), it is easy to see that we cannot have exactly $$$n-1$$$ same numbers on the diagonal. Note that if we fix the diagonals and add the numbers one by one, it becomes a standard matching problem so I just used flow to solve from there like in the editorial (after finding a greedy assignment for the diagonal).