Snow Chaos
Time limit1sMemory limit1024 MB
Given a path of n stations with s snowy edges and m travel requests, choose at most p snowy edges to clear so the number of requests whose endpoints become connected is maximized.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Intervals, Prefix sum, Brute force
- Solved
- No attempts yet
Problem
A snowstorm has moved in and traffic is in chaos. On top of that, parts of the railway track are snowed over, which has stopped all train traffic. This is of course not good, since crowds of travelers are now stuck at their stations with no way to get anywhere. Fredrika has therefore been tasked with clearing the snow between the stations before rush hour begins and the disaster is a fact.
The railway track has no branches at all and has stations, numbered from to in the order they appear on the track (so station comes just before station ). There are thus stretches between the stations. Of these, are covered in snow.
Fredrika is a little worried, because she has worked out that she only has time to clear the snow from stretches. To make the best of the situation, Fredrika decides to choose the stretches that let as many as possible of the waiting travelers get where they want to go. To help her, she has the answers from a survey she sent to everyone who is waiting, in which they answered which stations they want to travel between.
Assuming Fredrika chooses the stretches optimally, calculate how many waiting travelers will be able to travel once she is done.
Note that trains can only run on stretches without snow. Every station has trains parked at it, so every stretch without snow will be able to carry traffic, even if the stations cannot be reached from the endpoints of the track. Train traffic is completely stopped until Fredrika is done, so travelers who had a snow-free route from the start are also counted in the answer.

Figure 1: Sample 1
Input
The first line contains four integers, , , : the number of stations, the number of travelers, the number of snowed-over stretches, and the number of stretches Fredrika has time to plow.
Then follow lines, where line contains two integers , the stations where traveler wants to start and end.
Then follow lines, where line contains an integer , the station that lies just before snowed-over stretch . A stretch appears at most once in this list.
Output
Your program should print a single integer: the largest number of travelers who can complete their journeys by plowing at most stretches.