#P3530. [POI 2012] FES-Festival

[POI 2012] FES-Festival

Description

You do not know the results of the race, but Byteasar will tell you partial information. Now, you are asked to answer: what is the maximum number of distinct finishing times that all participants can have without violating his conditions? Some of them may tie (i.e., finish at the same time), and such ties count as one distinct time.

Byteasar tells you that all finishing times are integer seconds. He will also provide some relations between the participants’ times. Specifically, he will give you some pairs (A,B)(A, B), meaning that AA’s time is exactly 11 second less than BB’s; and some pairs (C,D)(C, D), meaning that CC’s time is not greater than DD’s. Your task is to compute the maximum possible number of distinct finishing times for all participants that satisfies these conditions.

Please write a program to solve this puzzle.

Input Format

The first line contains three integers n,m1,m2n, m_{1}, m_{2}, denoting the number of participants, the number of pairs (A,B)(A, B), and the number of pairs (C,D)(C, D), respectively.

The next m1m_{1} lines each contain two integers ai,bia_{i}, b_{i} (aibia_{i} \ne b_{i}). This means participant aia_{i}’s time is exactly 11 second less than participant bib_{i}’s.

The next m2m_{2} lines each contain two integers ci,dic_{i}, d_{i} (cidic_{i} \ne d_{i}). This means participant cic_{i}’s time is not greater than participant did_{i}’s.

Output Format

If there is a solution, output a single integer on one line, representing the maximum number of distinct finishing times among all participants.
If there is no solution, output NIE.

4 2 2
1 2
3 4
1 4
3 1

3

Hint

The answer is 33, with t3=1t_3 = 1, t1=t4=2t_1 = t_4 = 2, t2=3t_2 = 3.
(tit_i denotes the time spent by participant ii.)

Constraints

  • For 15%15\% of the testdata, n10n \le 10.
  • For 100%100\% of the testdata, 2n6002 \le n \le 600, 1m1+m21051 \le m_{1} + m_{2} \le 10^{5}.

Translated by ChatGPT 5