#P5382. Maximizing the Chat Group Ratio
Maximizing the Chat Group Ratio
Maximizing the Chat Group Ratio
In the chat group called "Improving Life", there are a total of topics numbered from to , and members (member is Xiao Z, and members through are his friends). Each topic is associated with a member (which could be Xiao Z himself, i.e. ) and if the topic appears in the chat, the interested member will engage in an intense discussion lasting minutes. Furthermore, there are guiding relations between topics. Each guiding relation is given as a pair meaning that if topic appears, then topic will definitely appear.
Since all of Xiao Z’s different topics (those with ) have no direct or indirect guiding relations among themselves, Xiao Z can choose to initiate one or more topics he is interested in (i.e. topics with ). Note that for any topic with , its appearance can only be triggered via a guiding relation. Once a topic is initiated, the chain of guidance will force its entire closure (i.e. all topics reachable via the guiding relations) to appear (each topic appears at most once even if triggered from multiple sources).
Let denote the total minutes of intense discussion by member (i.e. the sum of values for all appeared topics that member is interested in). Xiao Z wants to choose a nonempty subset of his own topics to initiate so that the ratio
[ R = \frac{\max\limits_{k=2}^{n} \mathrm{sum}(k)}{\mathrm{sum}(1)} ]
is as large as possible. Your task is to help him compute the maximum possible value of ( R ) (and output its value after taking the floor). In other words, among all valid choices of initiated topics (which must be among those with ), compute the closure of each chosen set, then calculate (\mathrm{sum}(1)) (the total minutes by Xiao Z) and, for each friend , (\mathrm{sum}(k)) (the total minutes by friend ). Then, let the ratio be the maximum over of (\mathrm{sum}(k)/\mathrm{sum}(1)). Output the floor of the maximum ratio that can be achieved.
All formulas are given in (\LaTeX) format. You can assume that every topic (even if appeared more than once via different chains) is only counted once. Also, note that if no topic of a friend appears, then his discussion time is considered as .
inputFormat
The first line contains two integers and , where is the number of topics (and also the number of group members) and is the number of guiding relations. The following lines each contains two integers and , indicating that topic is interesting to member and carries a weight of minutes for intense discussion. The next lines each contains two integers and , representing a guiding relation: if topic appears then topic will definitely appear.
outputFormat
Output a single integer, which is the maximum possible value (after taking the floor) of the ratio (R = \frac{\max_{k=2}^n \mathrm{sum}(k)}{\mathrm{sum}(1)}) that Xiao Z can achieve by initiating an appropriate nonempty subset of his own topics.
sample
3 1
1 3
2 4
1 2
1 2
1