#graph302. POI2012, Festival
POI2012, Festival
Festival
Problem Source
POI 2012
Problem Description
A charity event is being held in Byteburg, and you are one of the fundraisers. Unfortunately, you missed some interesting events, including a cross-country race.
The puzzle lover Byteasar promises that he will donate a large sum of money if you can solve his puzzle.
You do not know the result of the cross-country race, but Byteasar will tell you partial information. Now he asks you: what is the maximum number of different results all contestants can achieve without contradicting the conditions he gives? (Some of them may tie, i.e. arrive at the finish line at the same time; such a tie counts as only one result.)
Byteasar tells you that all contestants' results are integers in seconds. He also gives you some relations among contestants' results. Specifically: he gives you some pairs meaning 's result is exactly second faster than 's; and he gives you some pairs meaning 's result is no slower than 's (i.e. takes no more time). You must answer: what is the maximum number of different results all contestants can achieve without contradicting his conditions?
Please write a program to solve this puzzle.
Input Format
The first line contains three integers , denoting the number of contestants, the number of pairs , and the number of pairs , respectively.
The next lines each contain two integers (), meaning contestant 's result is exactly second less than contestant 's.
The next lines each contain two integers (), meaning contestant 's result is no worse than contestant 's (takes no more time).
It is guaranteed that and .
Output Format
If there is a solution, output one integer on a single line: the maximum number of different results all contestants can achieve. If there is no solution, output NIE.
Sample Input
4 2 2
1 2
3 4
1 4
3 1
Sample Output
3