I've seen quite a lot of people asking about hacking but I couldn't find any guide in codeforces so I decided to make this blog.
What is Hacking?
$$$\ $$$ In a codeforces round, test cases might not be enough to judge your code. It's very normal since we can't expect setters to consider every single wrong solution to add new test cases. In other words, if your code gets Accepted during the contest, this doesn't mean your submission is necessarily correct. To avoid incorrect solutions that get Accepted due to the lack of enough test cases to judge during the round we use Hacking.
$$$\ $$$ There are two types of Hacking, one of them is after the contest and the other is during the contest; but the general idea of hack is providing a test case where you think someone's solution fails. The judge checks this specific test case you provided and determine if the solution actually fails. (Comparing outputs with actually working pre-made jury code to check if it gets Wrong Answer verdict, or simply checking if it's getting TLE/CE...) After Hacking Phase ends, all submissions run once again with new test cases that are added during the hacking phase. Note that if your solution is wrong and no one hacked you, it doesn't mean your submissions won't fail!
During the Round (Div. 2, Div. 1 and mixed Rounds)
$$$\ $$$ Contests you can do hacks with this format are Div1, Div2, Global or Div1+2 contests. In this format, you can submit again to fix your mistake if you got hacked unless you locked your submission from the lock button. You can see the point for wrong/correct hacks from the right-down corner in the dashboard or any problem page. Successful hacks give 100 points while unsuccessful ones give -50 points. So if you cannot hack a solution, you will lose your points for nothing, so BE ABSOLUTELY SURE before doing something and think more.
Note that if you think you can solve more problems, I don't suggest you think about hacks, if you fail at doing hacks, you will only be wasting your time and decreasing your score. Hacking is the last choice if you don't feel any process at solving problems. Now let's look at how to see someone's code and submit a hack.
Only the latest Accepted submission counts, which means if you want to hack someone with multiple Accepted codes, you need to hack their latest Accepted code since other ones are useless as the penalty is also calculated from the latest Accepted code.
$$$\ $$$ To do hacks, you must go to the dashboard. After that, click the lock signature near your accepted solutions. Now you are ready to hack! Note that you must get Accepted before doing hacks in this problem.
Be careful! When you lock your solution, you won't be able to submit your locked problem again, so if you get hacked or find an edge case, you won't be able to submit again to the problem.
Tip: If you (or someone else) lock your solution, your score will be seen with bold font at the standings.
$$$\ $$$ After locking your solution, note that you won't be able to hack everyone. This means that you can only hack your roommates.
- Oh wait, what does roommate mean? At the beginning of contests, the system will assign you to a random room. You can see your roommates' standings from the Room Section. If it doesn't appear, go to Dashboard and now you can see it.
$$$\ $$$ In the standings of the room section, you will see the points of your roommates and if you double-click or ctrl+click on a passed solution, you will be able to see the image of the solution code. That's because you must not just copy-paste the solution to gain time for stress-testing and brute force all solutions. Under that, you will see the Hack button and can send tests according to the constraints. If it turns out to be too long, you can send a generator code.
$$$\ $$$ If your hack is not suitable for the constraints, you will get the verdict 'Invalid input'. If your hack gets the verdict 'Unsuccessful hacking attempt' if the defenders' code works correctly against your input, and finally gets 'Successful hacking attempt' if the code gives TLE or gives a different answer from the jury answer or anything else other than Accepted.
- Tip: Convert the image to text by typing while trying about the correctness of a solution. You can think about edge case and try it by your eyes but it can be a little dangerous so you can make a mistake (unless you are NOT ahmet23).
note: Sometimes you can't make hacks if pretests are too strong or the solution is too obvious. In addition, hacks can really be a lottery. One or more of your roommates' solutions must be wrong to hack. So I strongly suggest you look into more problems if you are able to solve them.
After the Round (Div. 3, Div. 4 and Educational Rounds)
$$$\ $$$ There is a 12-hour open hacking phase after the contest ends where all users can look at others' submissions and try to find a counter case to their solution and hack them. You don't get extra points for these hacks. After the open hacking phase ends all submissions run again with the new test cases that have been added to tests during the open hacking phase. Contests with this format are Div. 3, Div. 4 Rounds, and Educational Rounds.
$$$\ $$$ During a 12-hours hacking session, you will be able to see everyone's code, unlike during-contest hacks. You just can go standings page and click any solution you want. Now you can see the code as text and copy-paste it to your local computer and start some experiments on that lab rat as you wish :).
Note that you don't need to get AC at that problem to do hacks as these hacks won't give you any score.
Now let's touch on some tips to do hacks.
First, notice that you can sort solutions by running time. This gives the advantage to find slow codes. If the problem is about optimizing time or something else (you can understand it by problem), you can generate a test that causes worst-case and brute-force over slow codes and submit hacks one by one.
Second, can find an edge case that doesn't appear at pretests and brute force over random suspicious codes. Generally, ratings below the 1500s are doing mistakes commonly (it maybe can be a little unethical, but true) so you can look to especially their codes.
If the solution to a problem requires double or something else that might cause a lot of trouble to people and if the pretests are weak, it can be easy to hack. You can use the technique above.