#P5382. Maximizing the Chat Group Ratio

    ID: 18614 Type: Default 1000ms 256MiB

Maximizing the Chat Group Ratio

Maximizing the Chat Group Ratio

In the chat group called "Improving Life", there are a total of nn topics numbered from 11 to nn, and nn members (member 11 is Xiao Z, and members 22 through nn are his friends). Each topic ii is associated with a member cic_i (which could be Xiao Z himself, i.e. ci=1c_i=1) and if the topic appears in the chat, the interested member will engage in an intense discussion lasting wiw_i minutes. Furthermore, there are mm guiding relations between topics. Each guiding relation is given as a pair (u,v)(u,v) meaning that if topic uu appears, then topic vv will definitely appear.

Since all of Xiao Z’s different topics (those with ci=1c_i=1) 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 ci=1c_i=1). Note that for any topic with ci1c_i\neq1, 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 sum(k)\mathrm{sum}(k) denote the total minutes of intense discussion by member kk (i.e. the sum of ww values for all appeared topics that member kk 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 ci=1c_i=1), compute the closure of each chosen set, then calculate (\mathrm{sum}(1)) (the total minutes by Xiao Z) and, for each friend k2k\ge2, (\mathrm{sum}(k)) (the total minutes by friend kk). Then, let the ratio be the maximum over k2k\ge2 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 00.

inputFormat

The first line contains two integers nn and mm, where nn is the number of topics (and also the number of group members) and mm is the number of guiding relations. The following nn lines each contains two integers cic_i and wiw_i, indicating that topic ii is interesting to member cic_i and carries a weight of wiw_i minutes for intense discussion. The next mm lines each contains two integers uu and vv, representing a guiding relation: if topic uu appears then topic vv 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